Ruby | Hash replace() method
Hash#replace() : replace() is a Hash class method which replaces the content of one hash with other.
Syntax: Hash.replace()
Parameter: Hash values
Return: replaces the content of one hash with other.
Example #1 :
# Ruby code for Hash.replace() method # declaring Hash valuea = {a:100, b:200} # declaring Hash valueb = {a:100, c:300, b:200} # declaring Hash valuec = {a:100} # replace Valueputs "Hash a replace form : #{a.replace(b)}\n\n" puts "Hash b replace form : #{b.replace(c)}\n\n" puts "Hash c replace form : #{c.replace(a)}\n\n" |
Output :
Hash a replace form : {:a=>100, :c=>300, :b=>200}
Hash b replace form : {:a=>100}
Hash c replace form : {:a=>100, :c=>300, :b=>200}
Example #2 :
# Ruby code for Hash.replace() method # declaring Hash valuea = { "a" => 100, "b" => 200 } # declaring Hash valueb = {"a" => 100} # declaring Hash valuec = {"a" => 100, "c" => 300, "b" => 200} # replace Valueputs "Hash a replace form : #{a.replace(b)}\n\n" puts "Hash b replace form : #{b.replace(c)}\n\n" puts "Hash c replace form : #{c.replace(a)}\n\n" |
Output :
Hash a replace form : {"a"=>100}
Hash b replace form : {"a"=>100, "c"=>300, "b"=>200}
Hash c replace form : {"a"=>100}


