B
Benjohn Barnes
Hello everyone. I'm hoping to do something like this (and then
automate it up by putting something neat in to a mix in module)...
class Test
def xxx; [1]; end
alias xxx_old xxx
def xxx; xxx_old + [2]; end
alias xxx_old xxx
def xxx; xxx_old + [3]; end
alias xxx_old xxx
...
end
In real use, these various defs and aliases would be in different
files, but would often be putting things in to the same class. The
above doesn't work - I think it's leading to a non terminating
recursive function.
My first question is, "is it correct that this doesn't work?"
I'd like to use a mechanism like this to enable me to have a kind of
hookable function in a class. As I said, the alias process would be
encapsulated by waving my hands and working that out later, but
probably making use of method_defined.
The mechanism allows you to build up an event handler: you send an
event to a class instance, and any receivers that are registered for
that event (by having defined the method to pick it up) will get it.
Example hookable methods for my system will be: is_being_setup,
will_be_torn_down, has_recieved_message will_send_message.
It's worth noting that the will_be methods should probably be
executed in the reverse order from that in which they are defined!
I'm pretty sure this is a good plan, but...
Question: "Does this seem like a good plan?"
Finally, as this doesn't work, can you see a nicer way to implement
it? I think I could capture the function each time it gets defined,
and put it in to class state, but it would need to be per class, I
think, rather than just on in the base class. I've thought about
several variations, but none of them seem to be very nice at all.
Thank you for any thoughts you have,
Cheers,
Benjohn
automate it up by putting something neat in to a mix in module)...
class Test
def xxx; [1]; end
alias xxx_old xxx
def xxx; xxx_old + [2]; end
alias xxx_old xxx
def xxx; xxx_old + [3]; end
alias xxx_old xxx
...
end
In real use, these various defs and aliases would be in different
files, but would often be putting things in to the same class. The
above doesn't work - I think it's leading to a non terminating
recursive function.
My first question is, "is it correct that this doesn't work?"
I'd like to use a mechanism like this to enable me to have a kind of
hookable function in a class. As I said, the alias process would be
encapsulated by waving my hands and working that out later, but
probably making use of method_defined.
The mechanism allows you to build up an event handler: you send an
event to a class instance, and any receivers that are registered for
that event (by having defined the method to pick it up) will get it.
Example hookable methods for my system will be: is_being_setup,
will_be_torn_down, has_recieved_message will_send_message.
It's worth noting that the will_be methods should probably be
executed in the reverse order from that in which they are defined!
I'm pretty sure this is a good plan, but...
Question: "Does this seem like a good plan?"
Finally, as this doesn't work, can you see a nicer way to implement
it? I think I could capture the function each time it gets defined,
and put it in to class state, but it would need to be per class, I
think, rather than just on in the base class. I've thought about
several variations, but none of them seem to be very nice at all.
Thank you for any thoughts you have,
Cheers,
Benjohn