这个错误是什么意思?我无法使转换工作。假设英里转换为公里,公里转换为英里。我想知道我是否搞砸了计算。不知道电话是如何与班级没有联系的......谢谢
编码
class Distance
@@value
@@unit
def initialize(value, unit)
@@value = value
@@unit = unit
end
def convert_miles_to_km(miles)
return miles * 0.621371
end
def convert_km_to_miles(km)
return km * 1.60934
end
end
puts "Adding:"
d1 = Distance.new(1, "m")
d2 = Distance.new(1, "k")
puts d1 + d2
d1 = Distance.new(2, "M")
d2 = Distance.new(2, "m")
d3 = d1 + d2
puts d3
puts d3.value
puts d3.unit
输出应该是:
Adding:
1.621371 Miles
4 Miles
4
m
当我运行脚本时出现错误
C:\Users\Phillip\Desktop\Distance>ruby distance.rb
distance.rb:26:in `<main>': undefined method `+' for #<Distance:0x19c6008 @distance=1> (NoMethodError)