Skip to content

Instantly share code, notes, and snippets.

@Mon-Ouie
Created July 6, 2014 08:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Mon-Ouie/d66c0253863eeb79da4b to your computer and use it in GitHub Desktop.
Save Mon-Ouie/d66c0253863eeb79da4b to your computer and use it in GitHub Desktop.
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
Copy link

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