This page printed from:
panoptic.com/rking/Hook+Methods+of+Ruby
For Our King, Only
Recent Changes
/
Heat
/
Syntax
/
History
Hook Methods of Ruby
!! BasicObject#method_missing Signature: method_missing(*arg1) Invoked by Ruby when obj is sent a message it cannot handle. symbol is the symbol for the method called, and args are any arguments that were passed to it. By default, the interpreter raises an error when this method is called. However, it is possible to override the method to provide more dynamic behavior. If it is decided that a particular method should not be handled, then super should be called, so that ancestors can pick up the missing method. The example below creates a class Roman, which responds to methods with names consisting of roman numerals, returning the corresponding integer values. {{ class Roman def romanToInt(str) # ... end def method_missing(methId) str = methId.id2name romanToInt(str) end end r = Roman.new r.iv #=> 4 r.xxiii #=> 23 r.mm #=> 2000 }}
Password
Summary of changes
Erase cookies
/
Syntax
/
History
Last changed: 2012/12/06 10:59