Created
November 27, 2012 10:29
-
-
Save kschiess/4153521 to your computer and use it in GitHub Desktop.
Exploring the meaning of Ruby 1.9 respond_to_missing?
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 respond_to?(sym) | |
if sym == :foo | |
return true | |
else | |
super | |
end | |
end | |
end | |
class Bar | |
def respond_to_missing?(sym, p) | |
if sym == :foo | |
return true | |
else | |
super | |
end | |
end | |
end | |
[Foo, Bar].each do |klass| | |
[[:respond_to?, :foo], | |
[:respond_to_missing?, :foo, false], | |
[:method, :foo]].each do |method, *args| | |
r = begin | |
klass.new.send(method, *args) | |
rescue => ex | |
ex | |
end | |
puts "Calling #{klass}##{method.inspect}(#{args.join(', ')}) # => #{r.inspect}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment