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?

4 Answers

Your below code

def price @price = price # <~~ method name you just defined with `def` keyword. end 

Creates never stopable recursion,.

How can I make this code work the way it is without attr_accessor?

You need to write as

def price=(price) @price = price end def price @price end 
2

You need to do:

@price = self.price 

to differentiate between your object attribute price and your method parameter price.

One general reason this can happen is if you have some method that calls itself recursively (i.e. calls itself inside itself, which causes an infinite loop).

That's the problem I had that led to the error message.

In your case that's what's happening here:

def price @price = price end 

read_attribute is what you are looking for

def price @price = read_attribute(:price) end 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like