红宝石 |哈希替换()方法
Hash#replace() : replace()是一种 Hash 类方法,它用另一个散列的内容替换一个散列的内容。
Syntax: Hash.replace()
Parameter: Hash values
Return: replaces the content of one hash with other.
示例 #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}