Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Created August 4, 2012 06:36
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 myronmarston/3255162 to your computer and use it in GitHub Desktop.
Save myronmarston/3255162 to your computer and use it in GitHub Desktop.
➜ ruby --version
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]
➜ ruby respond_to_missing.rb
Using #method works:
foo_bar
But #method_defined? doesn't:
method defined: false
And Module#instance_method raises a NameError:
respond_to_missing.rb:20:in `instance_method': undefined method `foo_bar' for class `Foo' (NameError)
from respond_to_missing.rb:20:in `<main>'
class Foo
def method_missing(name, *args)
return super unless name =~ /^foo_/
puts name
end
def respond_to_missing?(name, include_private)
super || name =~ /^foo_/
end
end
puts "Using #method works: "
Foo.new.method(:foo_bar).call
puts "But #method_defined? doesn't:"
puts "method defined: #{Foo.method_defined?(:foo_bar)}"
puts "And Module#instance_method raises a NameError:"
Foo.instance_method(:foo_bar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment