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 Ruby Basics (Retired) How Ruby Works Input and Output

Quesiton

Instead of typing

name = "World" puts "Hello #{"World"}!"

I did it like this:

name = "World" puts "Hello" + name

Why is the #{} important, surely Im missing something

1 Answer

Actually, both ways do the same thing. Most of the time it's easier/more convenient to use #{} instead of string concatenation (a fancy way of saying adding strings together).

For example, it's easier to type out the first example then the second example in this script:

name = "Alex"

# Example 1
puts "Hello, #{name}! It's nice to meet you!"

# Example 2
puts "Hello, " + name + "! It's nice to meet you!"

Also, it's more understandable/more readable to use #{}.

An important note: In Ruby, there's two ways to create a string. One way is to wrap the string in single quotes ', and another way is to wrap the string in double quotes ". You only can use the #{} magic (called string interpolation) on double-quoted strings.

I hope this helps :grin:

Happy coding! :tada:

:dizzy: ~Alex :dizzy: