You can't call the overwritten method by name or keyword. That's one of the many reasons why monkey patching should be avoided and inheritance be preferred instead, since obviously you can call the overridden method.
Inheritance
So, if at all possible, you should prefer something like this:
class Foo
def bar
'Hello'
end
end
class ExtendedFoo < Foo
def bar
super + ' World'
end
end
ExtendedFoo.new.bar # => 'Hello World'
This works, if you control creation of the Foo
objects. Just change every place which creates a Foo
to instead create an ExtendedFoo
. This works even better if you use the Dependency Injection Design Pattern, the Factory Method Design Pattern, the Factory Design Pattern or something along those lines.
Delegation
If you do not control creation of the Foo
objects, for example because they are created by a framework that is outside of your control (like Ruby on Rails for example), then you could use the Wrapper Design Pattern:
require 'delegate'
class Foo
def bar
'Hello'
end
end
class WrappedFoo < DelegateClass(Foo)
def initialize(wrapped_foo)
super
end
def bar
super + ' World'
end
end
foo = Foo.new # this is not actually in your code, it comes from somewhere else
wrapped_foo = WrappedFoo.new(foo) # this is under your control
wrapped_foo.bar # => 'Hello World'
Basically, at the boundary of the system, where the Foo
object comes into your code, you wrap it into another object, and then use that object instead of the original one everywhere else in your code.
Mixin Inheritance (broken)
You might be tempted to do something like this:
class Foo
def bar
'Hello'
end
end
module FooExtensions
def bar
super + ' World'
end
end
class Foo
include FooExtensions
end
Unfortunately, that won't work. It's a good idea, because it uses inheritance, which means that you can use super
. However, include
inserts the mixin above the class in the inheritance hierarchy, which means that FooExtensions#bar
will never be called (and if it were called, the super
would not actually refer to Foo#bar
but rather to Object#bar
which doesn't exist), since Foo#bar
will always be found first.
alias_method
chain
The problem we are having with our monkey patching is that when we overwrite the method, the method is gone, so we cannot call it anymore. So, let's just make a backup copy!
class Foo
def bar
'Hello'
end
end
class Foo
alias_method :old_bar, :bar
def bar
old_bar + ' World'
end
end
Foo.new.bar # => 'Hello World'
Foo.new.old_bar # => 'Hello'
The problem with this is that we have now polluted the namespace with a superfluous old_bar
method. This method will show up in our documentation, it will show up in code completion in our IDEs, it will show up during reflection. Also, it still can be called, but presumably we monkey patched it, because we didn't like its behavior in the first place, so we might not want other people to call it.
Method Wrapping
So, the big question is: how can we hold on to the bar
method, without actually keeping around an actual method? The answer lies, as it does so often, in functional programming. We get a hold of the method as an actual object, and we use a closure (i.e. a block) to make sure that we and only we hold on to that object:
class Foo
def bar
'Hello'
end
end
class Foo
old_bar = instance_method(:bar)
define_method(:bar) do
old_bar.bind(self).() + ' World'
end
end
Foo.new.bar # => 'Hello World'
Of the methods that use actual monkey patching instead of inheritance or delegation, this is the cleanest: since old_bar
is just a local variable, it will go out of scope at the end of the class body, and it is impossible to access it from anywhere, even using reflection! And since define_method
takes a block, and blocks close over their surrounding lexical environment (which is why we are using define_method
instead of def
here), it (and only it) will still have access to old_bar
, even after it has gone out of scope.
Short explanation:
old_bar = instance_method(:bar)
Here we are wrapping the bar
method into an UnboundMethod
method object and assigning it to the local variable old_bar
. This means, we now have a way to hold on to bar
even after it has been overwritten.
old_bar.bind(self)
This is a bit tricky. Basically, in Ruby (and in pretty much all single-dispatch based OO languages), a method is bound to a specific receiver object, called self
in Ruby. In other words: a method always knows what object it was called on, it knows what its self
is. But, we grabbed the method directly from a class, how does it know what its self
is?
Well, it doesn't, which is why we need to bind
our UnboundMethod
to an object first, which will return a Method
object that we can then call. (UnboundMethod
s cannot be called, because they don't know what to do without knowing their self
.)
And what do we bind
it to? We simply bind
it to ourselves, that way it will behave exactly like the original bar
would have!
Lastly, we need to call the Method
that is returned from bind
. In Ruby 1.9, there is some nifty new syntax for that (.()
), but if you are on 1.8, you can simply use the call
method; that's what .()
gets translated to anyway.
Here are a couple of other questions, where some of those concepts are explained:
The Future: Ruby 2.0
In Ruby 2.0, there is a way to make this easier.
Module#prepend
: Mixin Inheritance (fixed)
I like this, because it just uses inheritance, without the need for new keywords, method combinators, new semantics or anything like that.
Remember above in the Mixin Inheritance (broken) section, when we said that the problem is that the mixin gets inserted above the class in the inheritance hierarchy? Well, Module#prepend
is simply a new method which does the same thing as Module#include
, except it mixes in the module directly below the class:
class Foo
def bar
'Hello'
end
end
module FooExtensions
def bar
super + ' World'
end
end
class Foo
prepend FooExtensions # the only change to above: prepend instead of include
end
Foo.new.bar # => 'Hello World'
This code actually works right now, if you get yourself a recent checkout of the trunk branch of the YARV source code and compile it.
Rebind module method
Binding of methods were available in earlier Ruby versions however they always had to be from same class or inheritor of the class. Since Ruby 2.0 you can rebind any module method
module Foo
def bar
"Hello #{who}"
end
end
class Car
def bar
'Different hello'
end
def who
'Tomas'
end
end
car = Car.new
car.bar # => 'Different hello'
Foo.instance_method(:bar).bind(car).call # => "Hello Tomas"
car.bar # => 'Different hello'
Competing Non-Ruby Ideas
Method Combinators
One idea is the idea of method combinators from CLOS. This is basically a very lightweight version of a subset of Aspect-Oriented Programming.
Using syntax like
class Foo
def bar:before
# will always run before bar, when bar is called
end
def bar:after
# will always run after bar, when bar is called
# may or may not be able to access and/or change bar's return value
end
end
you would be able to "hook into" the execution of the bar
method.
It is however not quite clear if and how you get access to bar
's return value within bar:after
. Maybe we could (ab)use the super
keyword?
class Foo
def bar
'Hello'
end
end
class Foo
def bar:after
super + ' World'
end
end
old
keyword
This idea adds a new keyword similar to super
, which allows you to call the overwritten method the same way super
lets you call the overridden method:
class Foo
def bar
'Hello'
end
end
class Foo
def bar
old + ' World'
end
end
Foo.new.bar # => 'Hello World'
The main problem with this is that it is backwards incompatible: if you have method called old
, you will no longer be able to call it!
redef
keyword
Similar to above, but instead of adding a new keyword for calling the overwritten method and leaving def
alone, we add a new keyword for redefining methods. This is backwards compatible, since the syntax currently is illegal anyway:
class Foo
def bar
'Hello'
end
end
class Foo
redef bar
old + ' World'
end
end
Foo.new.bar # => 'Hello World'
Instead of adding two new keywords, we could also redefine the meaning of super
inside redef
:
class Foo
def bar
'Hello'
end
end
class Foo
redef bar
super + ' World'
end
end
Foo.new.bar # => 'Hello World'