我真的不明白价格=(量)方法内下列条件测试的实际效用......这个条件测试真的做什么?
class Ticket
attr_accessor :price
def initialize(price)
@price=price
puts "The price is #{price}"
end
def price=(amount)
if (amount*100).to_i==amount*100
@price=amount
puts "The new price is #{amount}"
else
puts "The price seems to be malformed"
end
end
end
x=Ticket.new(21)
x.price=100.22
又如何能检查输入的格式不正确?我是初学者,对不起。我只是不能理解它背后的逻辑。
当遇到你不明白的代码时,特别是当它很简单时,最好的方法就是玩电脑。选择一组值发送到'price =',如int,各种值,字符串,nils等的浮点数。然后查看每个值。 – 2014-09-30 15:19:59