ruby - How to merge multiple hashes? -
right now, i'm merging 2 hashes this:
department_hash = self.parse_department html super_saver_hash = self.parse_super_saver html final_hash = department_hash.merge(super_saver_hash)
output:
{:department=>{"pet supplies"=>{"birds"=>16281, "cats"=>245512, "dogs"=>513926, "fish & aquatic pets"=>46811, "horses"=>14805, "insects"=>364, "reptiles & amphibians"=>5816, "small animals"=>19769}}, :super_saver=>{"free super saver shipping"=>126649}}
but want merge more in future. example:
department_hash = self.parse_department html super_saver_hash = self.parse_super_saver html categories_hash = self.parse_categories html
how merge multiple hashes?
you can below way using enumerable#inject
:
h = {} arr = [{:a=>"b"},{"c" => 2},{:a=>4,"c"=>"hi"}] arr.inject(h,:update) # => {:a=>4, "c"=>"hi"} arr.inject(:update) # => {:a=>4, "c"=>"hi"}
Comments
Post a Comment