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) Ruby Strings What are Strings?

Ashley Livingston
Ashley Livingston
6,931 Points

Just curious about the different methods of outputting a variable in ruby.

In the ruby Hello World exercise, it's suggested that we output our name variable with:

puts "Hello #{name}"

I skipped ahead, and wrote

puts "hello" + name

and that worked just fine for me. Just curious about matters of best practice, or if it comes down to preference, or if there is a specific reason to use the suggested method over the way I tried.

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

The #{name} way is very handy if you don't want to break the string, but works only with double quotes. If you use it with single quotes, it will interpret these symbols literally. Once you start using Rails and possibly other Ruby frameworks, you will use the #{name} way most often.