Created
September 21, 2011 16:46
-
-
Save Burgestrand/1232614 to your computer and use it in GitHub Desktop.
Method locator and ancestral heritage
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
require 'method_locator' | |
class A | |
def moo | |
"A" | |
end | |
end | |
class B < A | |
def moo | |
"B" | |
end | |
end | |
b = B.new | |
methods = b.methods_for(:moo).map do |method| | |
[method.owner, method.bind(b)] | |
end | |
methods = Hash[methods] | |
# Now ain’t this cool? | |
p methods[A].call | |
p methods[B].call |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Might be worth noting this code uses the method_locator gem: https://github.com/ryanlecompte/method_locator