Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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".