Define Instance Variable Outside of Method Defenition (ruby)

Posted by Ell on Stack Overflow See other posts from Stack Overflow or by Ell
Published on 2010-03-28T16:35:41Z Indexed on 2010/03/28 16:43 UTC
Read the original article Hit count: 337

Filed under:
|
|
|
|

Hi all, I am developing (well, trying to at least) a Game framework for the Ruby Gosu library. I have made a basic event system wherebye each Blocks::Event has a list of handlers and when the event is fired the methods are called. At the moment the way to implement an event is as follows:

class TestClass

    attr_accessor :on_close

    def initialize
        @on_close = Blocks::Event.new
    end

    def close
        @on_close.fire(self, Blocks::OnCloseArgs.new)
    end
end

But this method of implementing events seems rather long, my question is, how can I make a way so that when one wants an event in a class, they can just do this

class TestClass

    event :on_close

    def close
        @on_close.fire(self, Blocks::OnCloseArgs.new)
    end
end

Thanks in advance, ell.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about method