Skip to content

Instantly share code, notes, and snippets.

@dougbarth
Created October 1, 2008 17:48
Show Gist options
  • Save dougbarth/14137 to your computer and use it in GitHub Desktop.
Save dougbarth/14137 to your computer and use it in GitHub Desktop.
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