Ruby Hash replace()方法
Hash#replace() : replace()是一个Hash类的方法,它可以将一个哈希的内容替换成另一个。
语法。Hash.replace()
参数:哈希值
返回:用另一个哈希值替换一个哈希值的内容。
例子 #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"
输出:
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}
例子 #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"
输出:
Hash a replace form : {"a"=>100}
Hash b replace form : {"a"=>100, "c"=>300, "b"=>200}
Hash c replace form : {"a"=>100}