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

I did some research on Ruby on. But... (' ') || ( " ").

I came into a little lesson for Ruby. On this problem i was ask :

Iterators are a way to change our objects. The allow us to execute code on each item in an array or hash. This helps when we want to repeat an action many times on these objects.

Imagine we have a music collection and want to play each song in our collection. We could write an iterator like this:


music  =  ["modern", "classical", "folk"]

music.each  do  |song|
  puts  "play #{song}!"
end

I try this just to see what would happen. I was getting a error

"First output should be hello Edgar".

Notice I'm using Single Quote Marks ( ' ' ) :


names = ['Edgar', 'Gaston', 'Leo', 'Robert']
names.each do |friends|
  puts 'Hello #{friends}!'
end

But when i try it again with Double Quote Marks it runs without a problem.


names = ["Edgar", "Gaston", "Leo", "Robert"]
names.each do |friends|
  puts "Hello #{friends}!"
end

Can anyone explaing to me why this happens? I'm a person the likes to use Single quote marks. I would like to be hear other people opinion.

1 Answer

you cannot use interpolation ( #{} ) in single quote strings.

interpolation only works in double quote strings. so most of the time you want to use double quote strings for this reason.

"Hello #{friends}!

works fine because you are using double quotes, and interpolation works in double quotes.

 'Hello #{friends}!'

does not work because interpolation does not work in single quote strings

Thanks!