Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Method Missing
class Foo
MYMETHODS = ['van', 'car', 'bus']
def foo(x)
case x
when 'van' then return 'Long vehicle'
when 'car' then return 'Small vehicle'
when 'bus' then return 'Very Long vehicle'
else return 'None'
end
end
MYMETHODS.each do |method|
define_method method do
foo(method)
end
end
def method_missing(param)
return "Method #{param} is not defined"
end
end
o = Foo.new
puts o.van
puts o.car
puts o.bus
puts o.truck
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment