From: Phlip Date: 2008-12-28T00:59:49+09:00 Subject: Re: What does 'Monkey Patching' exactly Mean in Ruby? Yaser Sulaiman wrote: > 1- What does "runtime code" refer to in Ruby? In WikiPedia's world, all applications have code that executes for the end-user, and support code - such as compilers, test files, code generators, etc. Runtime code is the former. > 3- Is the following considered as monkey patching, or is it something else? > > class String > def foo > "bar" > end > end No, that is simply extending the class with a new method. Ruby classes are always open for extension, and the article you cited implied the misunderstanding was that is monkey patching. It is not. This is monkey patching: class String def length 42 end end The method .length already existed, but we had our way with it. And this example shows why MP is kind'a dangerous. Ruby has no mechanism to allow only our own modules to see our patches. My example gives all library code a bogus string length, and our runtime code would not last very long... Unfortunately for those who fear Monkey Patching, many Ruby libraries rely on it. They are released rough-and-ready, before any serious effort to detect where to install "hooks" (look up "Abstract Template Design Pattern"). So to get anything done in a big Ruby application, your monkeys will be frequently climbing up your inheritance trees and patching the code they find there! > [1]: http://en.wikipedia.org/wiki/Monkey_patch WP is full of biases and points-of-view; use it carefully!! -- Phlip