Created
May 25, 2011 01:51
-
-
Save captainpete/990168 to your computer and use it in GitHub Desktop.
Sample Hash unwrapper for Jon
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
def unwrap_hash(hash, scope = []) | |
hash.each do |key, value| | |
scope.push key | |
if value.is_a? Hash | |
unwrap_hash value, scope | |
else | |
puts "#{scope.join(':')} = #{value.inspect}" | |
end | |
scope.pop | |
end | |
end | |
unwrap_hash({ | |
:one => { | |
:apple => 1, | |
:pears => 14 | |
}, | |
:rabbit => { | |
:moonshine => 84, | |
:overture => 'Concerto number 4' | |
} | |
}) | |
# one:apple = 1 | |
# one:pears = 14 | |
# rabbit:overture = "Concerto number 4" | |
# rabbit:moonshine = 84 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment