Ruby-String.to_i错误
写一个年龄检查器程序,我正在添加代码来检查输入错误 我在理解一段代码的问题时遇到了一个问题,ruby为传递到一些字符串对象中的.to_I方法返回一个(NoMethodError) 完整代码如下Ruby-String.to_i错误,ruby,nomethoderror,Ruby,Nomethoderror,写一个年龄检查器程序,我正在添加代码来检查输入错误 我在理解一段代码的问题时遇到了一个问题,ruby为传递到一些字符串对象中的.to_I方法返回一个(NoMethodError) 完整代码如下 def calculate_difference(year,month,day) years = Time.new.year - year months = Time.new.month - month days = Time.new.day - day if months == 0
def calculate_difference(year,month,day)
years = Time.new.year - year
months = Time.new.month - month
days = Time.new.day - day
if months == 0
if days < 0
age = years - 1
elsif days >= 0
age = years
end
elsif months > 0
age = years
else
age = years - 1
end
end
puts "What year were you born in?"
year = gets.chomp
while year.to_i < 1900 or year.to_i > Time.new.year - 5
puts "Please enter a year between 1900 and 5 years ago"
year = gets.chomp
end
puts "And the month?"
month = gets.chomp
while month.to_i > 12 or month.to_i <= 0
puts "MONTH please, numbers from 1 to 12..."
month = gets.chomp
end
puts "And the day number?"
day = gets.chomp
我不确定它为什么会抛出错误,因为我确定字符串整数会响应.to_I方法
“=
”是分配。
“==
”正在检查它们是否相等,并返回true或false
if month.to_i == 1 or month.to_i == 3 or month.to_i == 5 or month.to_i == 7 or month.to_i == 8 or month.to_i == 10 or month.to_i == 12
while day.to_i > 31 or day.to_i <= 0
puts "Your month of birth has 31 days, please select a date within the correct range"
day = gets.chomp
end
end
if month.to_i == 2
while day.to_i > 28 or day.to_i < 0
puts "Incorrect day selected for February"
day = gets.chomp
end
end
如果month.to_i==1或month.to_i==3或month.to_i==5或month.to_i==7或month.to_i==8或month.to_i==10或month.to_i==12
而day.to_i>31或day.to_i 28或day.to_i<0
将“为二月选择的日期不正确”
day=get.chomp
结束
结束
或更具可读性:
if [1, 3, 5, 7, 8, 10, 12].include? month.to_i
while day.to_i > 31 or day.to_i <= 0
puts "Your month of birth has 31 days, please select a date within the correct range"
day = gets.chomp
end
end
if 2 == month.to_i
while day.to_i > 28 or day.to_i < 0
puts "Incorrect day selected for February"
day = gets.chomp
end
end
if[1,3,5,7,8,10,12]。是否包括?月至月
而day.to_i>31或day.to_i 28或day.to_i<0
将“为二月选择的日期不正确”
day=get.chomp
结束
结束
错误消息的内容是“未定义方法”到_i=
”,而不是“到_i
”
这是因为Ruby将单个“=”解释为您想要调用假设字段上的setter方法,以_i
:
a = "12"
a.to_i == 12
# returns true
a.to_i = 12
# throws undefined method to_i=
12 = a.to_i
# creates a syntax error with the better message "unexpected ="
# prevents such assigns-equals errors
12 == a.to_i
# returns true
当您应该使用
=
扩展@Ajedi32编写的内容时,您正在使用=
:双等号=
是相等运算符,用于确定两个值是否相等;这就是你想要的。单个equals=
是赋值运算符,用于将右侧的值赋值给左侧的变量。(有关最后一部分的更多信息,请参阅)扩展@iamnotmaynard所写的内容:所有条件都应写为const value equal variable
,这意味着所有lvalue
项都不能意外地指定新值item==0
bad choice,但是0==item
好多了注意,谢谢你的帮助这个答案只解释错误,不教或防止将来的错误抱歉我最近看到并修改了这个,很快会回来更新。谢谢大家的帮助,@bisounours tronconneuse谢谢你的提示这看起来更干净,可以用这种方式实现。正是@gaussblurinc建议了这种更可读的格式,并用这种格式编辑了我的答案,所以你应该感谢他;)我明白了,谢谢你!谢谢,这很有道理
if [1, 3, 5, 7, 8, 10, 12].include? month.to_i
while day.to_i > 31 or day.to_i <= 0
puts "Your month of birth has 31 days, please select a date within the correct range"
day = gets.chomp
end
end
if 2 == month.to_i
while day.to_i > 28 or day.to_i < 0
puts "Incorrect day selected for February"
day = gets.chomp
end
end
a = "12"
a.to_i == 12
# returns true
a.to_i = 12
# throws undefined method to_i=
12 = a.to_i
# creates a syntax error with the better message "unexpected ="
# prevents such assigns-equals errors
12 == a.to_i
# returns true