I have the following code:
@state = Hash.new
@state[:ran] = true
derp = Derp.new
NoMethodError: undefined method `=' for nil:NilClass
from (irb):4:in `run'
from /usr/local/bin/irb:11:in `<main>'
class Derp def initialize @state = Hash.new end def run @state[:ran] = true end end derp = Derp.new derp.run
The problem in your code is that in the way you did the hash is assigned to the instance variable @state of the class object Derp, not to Derp's objects. Instance variables of the class are different from instance variables of that class’s objects. You could use that variable in class methods. E.g.
class Derp @state = 42 def self.state @state end end puts Derp.state # 42