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