Method missing in Ruby

When you call a ruby method which doesn't exist, i.e if ruby object fails to find any such method, NoMethodError is raised. It can be avoided using a method avilable in ruby called Method Missing

To handle NoMethodError gracefully, Method missing can be used in ruby.Consider the example below

Class Sap
def first
puts "You are in first method"
end

def method_missing(method)
puts "There is no method called #{method} in class Sap"
end

end


ob = Sap.new
ob.frst

The output will be
"There is no method called frst in class Sap"

Post a Comment