Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine one hash with another hash in ruby

Tags:

hashmap

ruby

I have two hashes...

a = {:a => 5} b = {:b => 10} 

I want...

c = {:a => 5,:b => 10} 

How do I create hash c?

like image 799
thefonso Avatar asked Nov 30 '12 23:11

thefonso


People also ask

How do you combine hashes in Ruby?

We can merge two hashes using the merge() method. When using the merge() method: Each new entry is added to the end. Each duplicate-key entry's value overwrites the previous value.

How do you combine hash?

Hash#merge!() is a Hash class method which can add the content the given hash array to the other. Entries with duplicate keys are overwritten with the values from each other_hash successively if no block is given.

How do you make hash hash in Ruby?

Ruby hash creation. A hash can be created in two basic ways: with the new keyword or with the hash literal. The first script creates a hash and adds two key-value pairs into the hash object. A hash object is created.

Can a hash have multiple values Ruby?

Each key can only have one value. But the same value can occur more than once inside a Hash, while each key can occur only once.


2 Answers

It's a pretty straight-forward operation if you're just interleaving:

c = a.merge(b) 

If you want to actually add the values together, this would be a bit trickier, but not impossible:

c = a.dup b.each do |k, v|   c[k] ||= 0   c[k] += v end 

The reason for a.dup is to avoid mangling the values in the a hash, but if you don't care you could skip that part. The ||= is used to ensure it starts with a default of 0 as nil + 1 is not valid.

like image 56
tadman Avatar answered Oct 18 '22 19:10

tadman


(TL;DR: hash1.merge(hash2))

As everyone is saying you can use merge method to solve your problem. However there is slightly some problem with using the merge method. Here is why.

person1 = {"name" => "MarkZuckerberg",  "company_name" => "Facebook", "job" => "CEO"}  person2 = {"name" => "BillGates",  "company_name" => "Microsoft", "position" => "Chairman"} 

Take a look at these two fields name and company_name. Here name and company_name both are same in the two hashes(I mean the keys). Next job and position have different keys.

When you try to merge two hashes person1 and person2 If person1 and person2 keys are same? then the person2 key value will override the peron1 key value . Here the second hash will override the first hash fields because both are same. Here name and company name are same. See the result.

people  = person1.merge(person2)   Output:  {"name"=>"BillGates", "company_name"=>"Microsoft",          "job"=>"CEO", "position"=>"Chairman"} 

However if you don't want your second hash to override the first hash. You can do something like this

  people  = person1.merge(person2) {|key, old, new| old}    Output:   {"name"=>"MarkZuckerberg", "company_name"=>"Facebook",              "job"=>"CEO", "position"=>"Chairman"}  

It is just a quick note when using merge()

like image 20
Prabhakar Undurthi Avatar answered Oct 18 '22 21:10

Prabhakar Undurthi