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