首页 > 技术文章 > ruby the diference between gets and gets.chomp()

jerrychen 2015-07-16 14:44 原文

ruby the diference between gets and gets.chomp()

二者都是可以获取用户命令行输入的函数,但是 gets获取内容后,后面 附带了 多余的换行符号'\n'; 而gets.chomp() 是对后面多余的换行符'\n' 进行清除。
例如 ex11.rb:

print "How old are you? "
age = gets.chomp()
print "How tall are you? "
height = gets
print "How much do you weight? "
weight = gets.chomp()

puts "So, you're #{age} old , #{height} tall and #{weight} heavy."

然后输出:

注意身高的地方进行的换行。

推荐阅读