On 9/30/2010 2:53 PM, Alex S. wrote:
But is there a way to call the original method instead of just quitting
If you want to know if you can conditionally define the method and skip
the exit, yes, you can. Just leave out the else clause of the example.
That way, your method is called only if the original one didn’t already
exist. I used this once to implement Enumerable#none? only under
versions of Ruby where that method was not yet implemented.
If you want to have a kind of functionality similar to that provided by
the super keyword, you can do that too:
if method_defined?(:my_method) then
alias :my_method_orig :my_method
# Maybe do some extra stuff here.
# Call the original method if there is one.
if methods.include?("my_method_orig") then
# Maybe do more extra stuff here.
This will alias the original instance of my_method, if there is one, to
my_method_orig. Your new my_method definition can call the alias if it
exists and skip it otherwise. I suppose this would be good for
instrumenting methods to add logging and the like, but I would take care
with using this generally since it can make some unnecessarily