daremkd
daremkd

Reputation: 8424

Ruby: Why is this printed on a new line?

I have:

print 'Please enter the total amount of your bill: '
bill_amount = gets.chomp.to_f

print 'What percentage tip do you want? '
tip = gets.chomp.to_f / 100

puts "Your bill amount will be #{bill_amount * tip}"

When the final "puts" is printed, it's printed on a new line. Why is this? The previous "print something" statement was "print" which doesn't insert a new line. Even if I put "print" instead of "puts" for the end line, it still prints in a new line. What's going on? How can I make it so the end statement prints in the same line as "What percentage tip do you want"?

Upvotes: 0

Views: 192

Answers (2)

Aaditi Jain
Aaditi Jain

Reputation: 7027

Its printing in next line as after the user enters the value for 'Tip' , he/she has to press the 'enter' key to proceed.

The value of variable 'tip' will be free of any trailing white spaces (beacuse of .chomp). However on the screen, the enter key has already moved the user to the next line and hence the statement prints in the next line.

Upvotes: 0

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230286

It is printed on the next line because you press ENTER to finish inputing your data (and therefore you make a new line). Didn't it confuse you that second print also goes to a new line?

Upvotes: 2

Related Questions