Saturday, July 9, 2011

How to extend a class


class Symbol
  def %(arg)
    ...
  end
end
The above code does what you need, but leaves no hint that you've made a change to Symbol. An alternative solution is to define a module and include that module in Symbol.
module SymbolExtension
  def %(arg)
    ...
  end
end

Symbol.send :include, SymbolExtension


Granted, this isn't a huge hint, but if you check
 Symbol.ancestors you'll find the following list.
Symbol
SymbolExtension
Object
Kernel
extracted from: http://blog.jayfields.com/2007/01/class-reopening-hints.html

No comments:

Post a Comment