Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class Wrong
def method_missing(m, *)
if m =~ /\Ahello_(.+)\z/
puts "Hello, #{$1.capitalize}"
else
super
end
end
end
w = Wrong.new
w.hello_world
p w.respond_to? :hello_world
p defined? w.hello_world
begin
p w.method(:hello_world)
rescue NameError
puts "not defined!"
end
puts
class Right
def method_missing(m, *)
if m =~ /\Ahello_(.+)\z/
puts "Hello, #{$1.capitalize}"
else
super
end
end
def respond_to_missing?(m, include_all)
m =~ /\Ahello_(.+)\z/ || super
end
end
r = Right.new
r.hello_world
p r.respond_to? :hello_world
p defined? r.hello_world
begin
p r.method(:hello_world)
rescue NameError
puts "not defined!"
end
@aruprakshit

This comment has been minimized.

Copy link

@aruprakshit aruprakshit commented Jul 6, 2014

This is an awesome example, I have ever seen.. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment