Ruby – What does ‘Monkey Patching’ exactly Mean in Ruby

monkeypatchingrubyterminology

According to Wikipedia, a monkey patch is:

a way to extend or modify the runtime
code of dynamic languages […]
without altering the original source
code.

The following statement from the same entry confused me:

In Ruby, the term monkey patch was
misunderstood to mean any dynamic
modification to a class and is often
used as a synonym for dynamically
modifying any class at runtime.

I would like to know the exact meaning of monkey patching in Ruby. Is it doing something like the following, or is it something else?

class String
  def foo
    "foo"
  end
end

Best Answer

The best explanation I heard for Monkey patching/Duck-punching is by Patrick Ewing in RailsConf 2007

...if it walks like a duck and talks like a duck, it’s a duck, right? So if this duck is not giving you the noise that you want, you’ve got to just punch that duck until it returns what you expect.