Skip to content
Related Articles

Related Articles

Improve Article
Ruby | Hash replace() method
  • Last Updated : 07 Jan, 2020

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 value
a = {a:100, b:200}
  
# declaring Hash value
b = {a:100, c:300, b:200}
  
# declaring Hash value
c = {a:100}
  
  
# replace Value
puts "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 value
a = { "a" => 100, "b" => 200 }
  
# declaring Hash value
b = {"a" => 100}
  
# declaring Hash value
c = {"a" => 100, "c" => 300, "b" => 200}
  
  
# replace Value
puts "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}




My Personal Notes arrow_drop_up
Recommended Articles
Page :