Ruby | Hash merge function
Hash#merge() is a Hash class method which combines two hash arrays and their content.
Syntax: Hash.merge()
Parameter: Hash values
Return: combine two hash arrays
Example #1 :
# Ruby code for Hash.merge() method # declaring Hash value a = {a:100, b:200} # declaring Hash value b = {a:100, c:300, b:200} # declaring Hash value c = {a:100} # merge Value puts "Hash a merge form : #{a.merge(b)}\n\n" puts "Hash b merge form : #{b.merge(c)}\n\n" puts "Hash c merge form : #{c.merge(a)}\n\n" |
chevron_right
filter_none
Output :
Hash a merge form : {:a=>100, :b=>200, :c=>300}
Hash b merge form : {:a=>100, :c=>300, :b=>200}
Hash c merge form : {:a=>100, :b=>200}
Example #2 :
# Ruby code for Hash.merge() method # declaring Hash value a = { "a" => 100, "b" => 200 } # declaring Hash value b = {"a" => 100} # declaring Hash value c = {"a" => 100, "c" => 300, "b" => 200} # merge Value puts "Hash a merge form : #{a.merge(b)}\n\n" puts "Hash b merge form : #{b.merge(c)}\n\n" puts "Hash c merge form : #{c.merge(a)}\n\n" |
chevron_right
filter_none
Output :
Hash a merge form : {"a"=>100, "b"=>200}
Hash b merge form : {"a"=>100, "c"=>300, "b"=>200}
Hash c merge form : {"a"=>100, "c"=>300, "b"=>200}
Recommended Posts:
- Ruby | Hash merge! function
- Ruby | Hash key?() function
- Ruby | Hash any?() function
- Ruby | Hash dig() function
- Ruby | Hash eql? function
- Ruby | Hash key() function
- Ruby | Hash each() function
- Ruby | Hash keep_if() function
- Ruby | Hash values function
- Ruby | Hash invert() function
- Ruby | Hash each_pair() function
- Ruby | Hash compare_by_identity? () function
- Ruby | Hash has_value?() function
- Ruby | Hash compact!() function
- Ruby | Hash flatten() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



