-
-
Save PatrickTulskie/269735 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
# 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