On 9/30/2010 2:53 PM, Alex Stahl wrote:
> But is there a way to call the original method instead of just quitting
> out?

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:

class String
  if method_defined?(:my_method) then
    alias :my_method_orig :my_method
  end

  def my_method
    # Maybe do some extra stuff here.
    ...

    # Call the original method if there is one.
    if methods.include?("my_method_orig") then
      my_method_orig
    end

    # Maybe do more extra stuff here.
    ...
  end
end

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
complicated code.

-Jeremy