Gets.chomp无法正常工作

时间:2016-07-26 12:43:38

标签: ruby

我正在做第11次学习Ruby艰难之路的练习。我必须得到这个:

How old are you? 35
How tall are you? 6'2"
How much do you weigh? 180lbs
So, you're 35 old, 6'2" tall and 180lbs heavy.

写这个:

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

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

但我明白了:

How old are you? 
How tall are you? 
How much do you weigh? 
So, you're  old,  tall and  heavy.

这意味着gets.chomp无效,而且我什么都不懂。

1 个答案:

答案 0 :(得分:4)

在运行程序后出现提示时,您应该输入值,而不仅仅是输入:

# ⇓   prompt ⇓   ⇓⇓ YOUR INPUT!!! 
How old are you? 35

Sidenote chomp之后的括号是多余的,而不是ruby惯用语。