Print variables in Ruby

Asked

Viewed 1,008 times

1

How to print two variables on the same line (without skipping)? I’ve tried print instead of puts but it didn’t work.

  • Can you give an example of what you did? for me the print works http://ideone.com/PgRQpq

  • 2

    I will repeat, you must [Edit] the question and put the code you are using.

3 answers

2


Use the concatenation character +

print variavel1 + " " + variavel2

or

you can use this way to interpret variables within a string as well:

print "Primeira variavel: #{variavel1} Segunda variavel: #{variavel2}"

or

you can pass as if they were 2 parameters

print variavel1,variavel2
  • Just a complement, give preference to the second way, instead of the first is much more performative and is a good practice.

1

  • It works if the variable is initialized with defined values, but if I put a gets happens this problem I quoted

  • well, first check if you have a line break in your string. See if this solves your problem: print variable1.delete(' n'),variable2

0

I used a gets.chomp and it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.