Skip to content

Instantly share code, notes, and snippets.

@PatrickTulskie
Forked from jamster/hash_inspection.rb
Created January 5, 2010 21:14
Show Gist options
  • Save PatrickTulskie/269735 to your computer and use it in GitHub Desktop.
Save PatrickTulskie/269735 to your computer and use it in GitHub Desktop.
# An extension to the Hash class to inspect what it is made of
class Hash
def whatami
keys.inject({}) do |hash, key|
hash[key] = case self[key].class.to_s
when "Hash"
self[key].whatami
when "Array"
self[key].map do |item|
item.is_a?(Hash) ? item.whatami : item.class.to_s
end
else
self[key].class.to_s
end
hash
end
end
end
# TESTING
require "rubygems"
require "activesupport"
testing = {
:hash => {:good => "bad"},
:number => 123123,
:string => "Hello",
:arry => [1, 2, 3, {:inside_a_hash => {:inside_another => "what?"}}]
}
puts testing.whatami.to_yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment