Created
August 4, 2012 06:36
-
-
Save myronmarston/3255162 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
➜ 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>' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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