Created
March 9, 2010 12:31
-
-
Save sixtyfive/326528 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
# Adapted from the recently uploaded Rails initializer file for | |
# loading the content of app_config.yml into a variables.sass file. | |
# | |
# Useful for: | |
# | |
# Flattening a Hash, i.e.: | |
# irb> {:foo => :bar, :foobar => {:foo => :bar}}.flatten | |
# => {"foobar_foo"=>:bar, "foo"=>:bar} | |
class Hash | |
def flatten(input = nil, prefix = nil) | |
input = (input || self) | |
output = {} | |
input.each do |key,value| | |
if value.class == Hash | |
output = output.merge(flatten(value, key)) | |
else | |
output["#{prefix ? prefix.to_s+'_' : nil}#{key}"] = value | |
end | |
end | |
return output | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment