为什么在我的变量之后打印的文本出现在新行上?

时间:2018-04-02 12:13:42

标签: ruby

puts "Hey what's your name?!!"
name = gets
puts "#outputNAME: #{name}"
puts "Hey #{name}! Howdy doin'!"
puts "Tell us 2 numbers that add up to your age!"
age1 = gets.to_i
age2 = gets.to_i
puts "Hey! You're #{age1 + age2} years old! Gotcha!"
puts "I'm totally new to Ruby on Rails, so thanks for running my first Ruby 
Program. Please do leave an upvote and I'll appreciate that."
puts "If you think I need a downvote, please do state, in the comments 
section, what I could have done to make my program better (my 1st program, 
remember that #{name}). Also, check out my HTML pages, as I'm an ace at 
HTML."

print "Thanks, once again #{name} and stay tuned (especially for my HTML 
projects)!"

在这部分中,在#{name}之后,句子继续在一个新行上,我希望不会发生。是否有任何解释可能是我的程序中的错误,还是语言中的错误?请帮助我,因为我正在做一个单独的项目。 感谢无论谁找到解决这个问题的方法。

1 个答案:

答案 0 :(得分:4)

它会在name之后将文本打印到新行,因为您接受以下代码中的换行符。

name = gets

name = gets
raj
 => "raj\n" 

您可以按chomp方法删除此类换行符。

name = gets.chomp
raj
 => "raj"