Open In App

Ruby | Hash replace() method

Last Updated : 07 Jan, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
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
# 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
# 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}


Next Article

Similar Reads