代码中的 ruby 错误:43:Fixnum 的未定义方法 'to' (NoMethodError)



我是ruby新手,现在做了一些tut,我从tut中记下了一些代码,但我得到一个错误:

ruby1.rb:9:in `<main>': undefined method `to' for 43:Fixnum (NoMethodError)

这是我的代码:

print "Enter a Value:"
first_num = gets.to_i
print "Enter another value:"
second_num = gets.to_i
puts first_num.to.s + " + " + second_num.to_s  + " = " +
(first_num + second_num).to_s

就像

puts first_num.to_s + " + " + second_num.to_s + " = " + (first_num + second_num).to_s

这里有个错误,不是to_s而是to.s

你的第一个。S应该是to_s

试试这个

puts "#{first_num} + #{second_num}  =  #{first_num + second_num}"

最新更新