Skip to content

Instantly share code, notes, and snippets.

@seanbehan
Created January 6, 2012 17:28
Show Gist options
  • Save seanbehan/1571559 to your computer and use it in GitHub Desktop.
Save seanbehan/1571559 to your computer and use it in GitHub Desktop.
require 'yaml'
# Nested YAML structure from something-nested.yml
#
# nested:
# key: value
# key_two: value_two
# Procedural Approach to building a single Hash from nested YAML data structure.
@yaml = YAML.load_file("something-nested.yml")
@container = {}
@yaml.values.each do |value|
@container.merge!(value)
end
@container.inspect
# => {:key => value, :key_two => value_two}
# And a one liner...
@yaml.values.inject({}) { |container, value| container.merge(value) }
# => {:key => value, :key_two => value_two}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment