July 2011
Intermediate to advanced
400 pages
8h 46m
English
When Ruby tries to execute an undefined method (or, in object-oriented terms, when an object is sent a message that it cannot handle), the error causes the program to exit. You may prefer your program to recover from such an error. You can do this by writing a method named method_missing, with an argument to which the missing method’s name is assigned. This will execute when a nonexistent method is called:
nomethod1.rb
def method_missing( methodname )
puts( "Sorry, #{methodname} does not exist" )
end
xxx #=> Sorry, xxx does not existThe method_missing method can also take a list of incoming arguments (*args) after the missing method name:
nomethod2.rb
def method_missing( methodname, *args ) puts( "Class #{self.class} does ...Read now
Unlock full access