Ruby, stack level too deep (SystemStackError)

Ruby, stack level too deep (SystemStackError)

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

You need to do:

@price = self.price

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

Ruby, stack level too deep (SystemStackError)

read_attribute is what you are looking for

def price 
  @price = read_attribute(:price)
end

Leave a Reply

Your email address will not be published.