Skip to content

Instantly share code, notes, and snippets.

@kschiess
Created November 27, 2012 10:29
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 kschiess/4153521 to your computer and use it in GitHub Desktop.
Save kschiess/4153521 to your computer and use it in GitHub Desktop.
Exploring the meaning of Ruby 1.9 respond_to_missing?
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