Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Miscellaneous Ruby Questions

I'm doing the Learn Ruby track and have a couple of basic questions.

--I'm not sure what purpose exclamation points serve when coupled with a variable. --I don't understand the purpose of single quotes (I know double quotes signify strings). When are single quotes necessary, and when are they unnecessary?

I'll have other questions down the line, but this is all for now. Thank you!

2 Answers

Anthony Wijaya
Anthony Wijaya
3,215 Points

Hi Heather,

  1. Exclamation Marks:
a = "Some String"
a.upcase! 
# a will now print "SOME STRING" 
# now consider this version:
b = "Some String"
b.upcase
#b will print "Some String"

You can think of exclamation marks as a way to change the variable as well instead of doing it like this:

b = "A String"
b = b.upcase
# b will now print "A STRING"
  1. Single Quotes

Single quotes are basically the same as double quotes, however there are cases where you might want to use single quotes instead of double quotes. Right off my head there are

c = " My name is 'Programmer' "
# notice the single quote between programmer
# c will print => My name is 'Programmer'

# vs doing this instead

d = 'My name is 'Programmer''
# this won't work because it will think that the string ends with 'My name is '.


# double quotes also gives you string interpolation and escaping capability

e = 3
puts "#{e}" #prints 3


puts 'a\nb' # prints => a\nb 
puts "a\nb" # print a, then b at newline 

Hope this helps :D

Hi Anthony, thanks for your response! For clarification, I meant when exclamation points come in front of a variable.

John Fisher
John Fisher
7,974 Points

I think it helps when you're starting out on the Ruby track, just to think of Exclamation marks when in front of something to mean "Not".