Skip to content

Instantly share code, notes, and snippets.

@sixtyfive
Created March 9, 2010 12:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sixtyfive/326528 to your computer and use it in GitHub Desktop.
Save sixtyfive/326528 to your computer and use it in GitHub Desktop.
# 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