Responding to Calls to Undefined Methods : method_missing « Reflection « Ruby






Responding to Calls to Undefined Methods


class MyClass
  def defined_method
    'This method is defined.'
  end

  def method_missing(m, *args)
    "Sorry, I don't know about any #{m} method."
  end
end

o = MyClass.new
o.defined_method                         # => "This method is defined."
o.undefined_method
# => "Sorry, I don't know about any undefined_method method."

 








Related examples in the same category

1.Convert all system command to all class method
2.Override method_missing method to provide meaningful error message
3.Create a new class and override the method_missing method
4.If method does not exist, call the default one
5.Create new method dynamically