Ruby - 字符串 .to_i 错误
Ruby - String .to_i error
正在编写年龄检查程序,正在添加代码以检查输入错误。
我无法理解一段代码的问题所在,ruby returns a (NoMethodError) for .to_i 方法传递到一些字符串对象中。
完整代码如下;
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
#有问题的代码块:
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
stringdate = year+month+day
stringdate = stringdate.gsub(" ","")
while stringdate.length != 8
puts "in number format please: yyyy/mm/dd"
puts "What year were you born in?"
year = gets.chomp
puts "And the month?"
month = gets.chomp
puts = "And the day number?"
day = gets.chomp
end
year = year.to_i
month = month.to_i
day = day.to_i
puts "Congratulations, you are #{calculate_difference(year,month,day)} years old."
我不确定为什么会返回错误,因为我确定字符串整数会响应 .to_i 方法。
"=
" 是赋值。
“==
”正在检查它们是否相等以及 returns 真或假。
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
或更具可读性:
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
错误消息显示为 "undefined method 'to_i=
'" 而不是“to_i
”
这是因为 Ruby 将单个“=”解释为好像您想在假设字段上调用 setter 方法 to_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
正在编写年龄检查程序,正在添加代码以检查输入错误。
我无法理解一段代码的问题所在,ruby returns a (NoMethodError) for .to_i 方法传递到一些字符串对象中。
完整代码如下;
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
#有问题的代码块:
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
stringdate = year+month+day
stringdate = stringdate.gsub(" ","")
while stringdate.length != 8
puts "in number format please: yyyy/mm/dd"
puts "What year were you born in?"
year = gets.chomp
puts "And the month?"
month = gets.chomp
puts = "And the day number?"
day = gets.chomp
end
year = year.to_i
month = month.to_i
day = day.to_i
puts "Congratulations, you are #{calculate_difference(year,month,day)} years old."
我不确定为什么会返回错误,因为我确定字符串整数会响应 .to_i 方法。
"=
" 是赋值。
“==
”正在检查它们是否相等以及 returns 真或假。
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
或更具可读性:
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
错误消息显示为 "undefined method 'to_i=
'" 而不是“to_i
”
这是因为 Ruby 将单个“=”解释为好像您想在假设字段上调用 setter 方法 to_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