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."

然后输出:

ruby  the diference between gets and gets.chomp()-LMLPHP

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

05-11 14:02