Created
October 1, 2008 17:48
-
-
Save dougbarth/14137 to your computer and use it in GitHub Desktop.
This file contains 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
1 class Object | |
2 # Returns a hash where the key is a message that this object | |
3 # responds to and the value is the first object that responded | |
4 # to that message. | |
5 def first_responders | |
6 methods_hash = {} | |
7 self.class.instance_methods.each {|m| methods_hash[m] = self} | |
8 self.class.ancestors.each do |ancestor| | |
9 ancestor.instance_methods.each {|m| methods_hash[m] = ancestor} | |
10 end | |
11 methods_hash | |
12 end | |
13 end | |
14 | |
15 def dump_hash(hash) | |
16 dump = "{\n" | |
17 hash.each_pair do |key, value| | |
18 dump << "#{key.inspect} => #{value.inspect}\n" | |
19 end | |
20 dump << "}" | |
21 end | |
22 | |
23 puts "'abc'.first_reponders => #{dump_hash('abc'.first_responders)}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment