Page 1 of 5
Callbacks in Ruby?
Posted: Tue Jan 27, 2015 11:04 pm
by Exo
I would like to implement a callback system in Ruby.
Here is a simple example of what I mean...
This doesn't work because the output method is not recognized within the class.
The benefits of this should be obvious we could send data from anywhere in the schematic to anywhere else, like sending data from low down in the hierarchy back to the top (which is what I intend to do)
So is there a way to get this to work?
Re: Callbacks in Ruby?
Posted: Wed Jan 28, 2015 12:22 am
by KG_is_back
I believe the problem is, that "output" is a function of RubyEdit class. And the Callback class you've created doesn't inherit from RubyEdit, which means "output" is not declared for it.
In order for it to work, you have to send it the module it resides in when initialized... look at this fix:
Code: Select all
class Callback
def initialize(mod)
@module=mod
end
def doSomething()
@module.output "go" , nil
end
end
@call = Callback.new(self)
output "callback", @call
Re: Callbacks in Ruby?
Posted: Wed Jan 28, 2015 5:57 am
by RJHollins
alright .... you Guys are amazing
Even if I don't understand the how or why ... it's still is fascinating to try and follow new techniques.
Thanks !!

Re: Callbacks in Ruby?
Posted: Wed Jan 28, 2015 7:45 am
by Exo
Ah yes thanks KG. I had a brain freeze, should have realised output was part of RubyEdit.
Thanks.
Re: Callbacks in Ruby?
Posted: Wed Jan 28, 2015 12:19 pm
by Tronic
why not just use the #send __send__ methods?
Re: Callbacks in Ruby?
Posted: Wed Jan 28, 2015 12:32 pm
by Exo
I wasn't aware of these methods. Can you give an example?
I'm not well versed in Ruby so I tend to default to doing things the 'Java' way.
Re: Callbacks in Ruby?
Posted: Wed Jan 28, 2015 3:54 pm
by Tronic
This is an example of
__send__ method
I used the
$intern_this gloabal variable to get the local instance of RubyEdit Class
to inherit it's intenal declared methods.
Re: Callbacks in Ruby?
Posted: Wed Jan 28, 2015 5:01 pm
by MyCo
Tronic wrote:I used the $intern_this gloabal variable to get the local instance of RubyEdit Class
to inherit it's intenal declared methods.
This doesn't work, because $intern_this seems to point to the last edited Ruby-Edit, not to the class that contains the code.
Re: Callbacks in Ruby?
Posted: Wed Jan 28, 2015 5:22 pm
by Tronic
yes you're right, but I think it's a problem of FS, for as the module is initialized,
but if you retrig the class it works correctly.
the $intern_this, it seems to me, point to the current RubyEdit module.
Edit: work only for the last call of Callback class, so Myco you you're right.
Re: Callbacks in Ruby?
Posted: Wed Jan 28, 2015 6:49 pm
by Tronic
However my example of callback with the method __send__ works well,
the only problem is if you want to inherit the methods of class RubyEdit.