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 Strings String Concatenation

Manuel Cano
Manuel Cano
1,058 Points

Whats the difference between using commas and + when you use print or puts

So in the first set of videos I saw we were using commas to space out printing variables. For example.

puts ‘what is your name’ var1 = gets puts what is your last name var2 = gets

puts var1 , var2 . What is so different from using the commas to doing something like puts var1 +” “ + var2 can you even use them interchangeably ?

1 Answer

Using a comma will print 1 variable then the next. If you use "puts var1, var2" this will print each variable onto a different line.

If you use puts "var1 + var2" this will concatenate the variables together onto the same line.

Also if you use + when the variable types are different it will return an error. (If var1 is a string & var2 is a number)

As I see they are similar but not interchangeable