[:initial_amount, :rate_increase_amount].each do |method|
define_method method do
self["#{method}_in_cents".to_sym].to_f/100 if self["#{method}_in_cents".to_sym]
end
define_method "#{method}=" do |_value|
self["#{method}_in_cents".to_sym] = _value.to_f * 100
end
end
它给出了以下错误:
NoMethodError: undefined method `initial_amount_in_cents' for #<ViolationType:0x6220a88>
我试图将其重写为:
def initial_amount_in_cents
initial_amount_in_cents.to_f/100 if initial_amount_in_cents
end
def rate_increase_amount_in_cents
rate_increase_amount_in_cents.to_f/100 if rate_increase_amount_in_cents
end
def initial_amount= (value)
initial_amount_in_cents = value.to_f * 100
end
def rate_increase_amount= (value)
rate_increase_amount_in_cents = value.to_f * 100
end
但它给了我这个错误:
ERROR SystemStackError: stack level too deep