Ruby, stack level too deep (SystemStackError)
I have the following code:
class BookPrice
attr_accessor :price
def initialize(price)
@price = price
end
def price_in_cents
Integer(price*100 + 0.5)
end
end
b = BookPrice.new(2.20)
puts b.price_in_cents
This all works well and produces 220. But when I replace the second line
attr_accessor :price with:
def price
@price = price
end
I get stack level too deep (SystemStackError) error. What's going on? I
know I can replace Integer(price*100 + 0.5) with @price instead of the
method call price, but I want to keep it the way it is for OOP reasons.
How can I make this code work the way it is without attr_accessor?
No comments:
Post a Comment