Skip to content

Instantly share code, notes, and snippets.

@jhiemer
Created March 1, 2014 17:44
Show Gist options
  • Save jhiemer/9293869 to your computer and use it in GitHub Desktop.
Save jhiemer/9293869 to your computer and use it in GitHub Desktop.
[{ "max" : {
"label" : "Maximum",
"value" : "max"
}
},{ "min" : {
"label" : "Minimum",
"value" : "min"
}
},{ "avg" : {
"label" : "Average",
"value" : "avg"
}
}, { "sum" : {
"label" : "Sum",
"value" : "sum"
}
}]
@havenwood
Copy link

require 'yaml/store'

store = YAML::Store.new 'yaml.store'

hash = {"max"=>{"label"=>"Maximum", "value"=>"max"},
            "min"=>{"label"=>"Minimum", "value"=>"min"},
            "avg"=>{"label"=>"Average", "value"=>"avg"},
            "sum"=>{"label"=>"Sum", "value"=>"sum"}}

store.transaction { hash.each { |k, v| store[k] = v } }

# Your hash is now persisted to './yaml.store'

store.transaction do
  ["max", "min", "avg", "sum"].each do |key|
    Object.const_set key.capitalize, store[key]
  end
end

Min
#=> {"label"=>"Minimum", "value"=>"min"}

Max
#=> {"label"=>"Maximum", "value"=>"max"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment