例子1:
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."
输出的内容如下所示:
How old are you ?11
How tall are you ?23
How much do you weight ?23
so, you're 11 old, 23
tall and 23 heavy.
例子2:
print "How old are you ?"
age = gets.chomp()
print "How tall are you ?"
height = gets.chomp()
print "How much do you weight ?"
weight = gets.chomp()
puts "so, you're #{age} old, #{height} tall and #{weight} heavy."
输出内容如下:
How old are you ?11
How tall are you ?23
How much do you weight ?23
so, you're 11 old, 23 tall and 23 heavy.
总结:gets和gets.chomp的区别在于,gets的input中包括\n,而gets.chomp的input中不包括\n。