Skip to content

Instantly share code, notes, and snippets.

@aruprakshit
Forked from Mon-Ouie/respond_to_missing.rb
Created July 6, 2014 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aruprakshit/ad528d6469db2d31a481 to your computer and use it in GitHub Desktop.
Save aruprakshit/ad528d6469db2d31a481 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment