title: Ruby: How to set instance variables from a class method?
body: <p>I'm not sure that's the right title for this question, but I don't know how else to ask it. I have classes that need to be registered globally so they can be called later. I have most of it working except for a very important part. When the child inherits from the parent class, it registers a new instance, but when the <code>on_message</code> class method is called, I can't figure out how to set the instance variables that I need.</p>
  
  <pre><code>class MyExtension &lt; ExtensionBase
  
    on_message '/join (.+)' do |username|
      # this will be a callback function used later
    end
  
  end
  
  class ExtensionBase
  
    def self.inherited(child)
      MainAppModule.registered_extensions &lt;&lt; child.new
    end
  
    def self.on_message(string, &amp;block)
      # these need to be set on child instance
      @regex = Regexp.new(string)
      @on_message_callback = block
    end
  
    def exec(message)
      args = @regex.match(message).captures
      @on_message_callback.call(args)
    end
  
  end
  
  # somewhere else in the code, I find the class that I need...
  
  MainAppModule.registered_extensions.each do |child|
      puts child.regex.inspect # this is nil and I dont want it to be
      if message =~ child.regex
        return child.exec(message)
      end
  end
  </code></pre>
  
  <p>How can I design this so that the <code>@regex</code> will be set so I can access it within the loop?</p>