📜  红宝石 |哈希存储()方法

📅  最后修改于: 2022-05-13 01:54:57.941000             🧑  作者: Mango

红宝石 |哈希存储()方法

Hash#store()是一个 Hash 类方法,它返回一个附加值,其键值由 key-value 参数给出。

示例 #1:

Ruby
# Ruby code for Hash.store() 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}
 
 
# store Value
puts "Hash a store form : #{a.store('e', 67)}\n\n"
 
puts "Hash b store form : #{b.store('d', 467)}\n\n"
 
puts "Hash c store form : #{c.store('g', 647)}\n\n"


Ruby
# Ruby code for Hash.store() 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}
 
# store Value
puts "Hash a store form : #{a.store('e', 67)}\n\n"
 
puts "Hash b store form : #{b.store('d', 467)}\n\n"
 
puts "Hash c store form : #{c.store('g', 647)}\n\n"
 
puts (a)
 
puts (b)
 
puts (c)


输出 :

Hash a store form : 67

Hash b store form : 467

Hash c store form : 647

示例 #2:

红宝石

# Ruby code for Hash.store() 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}
 
# store Value
puts "Hash a store form : #{a.store('e', 67)}\n\n"
 
puts "Hash b store form : #{b.store('d', 467)}\n\n"
 
puts "Hash c store form : #{c.store('g', 647)}\n\n"
 
puts (a)
 
puts (b)
 
puts (c)

输出 :

Hash a store form : 67

Hash b store form : 467

Hash c store form : 647

{"a"=>100, "b"=>200, "e"=>67}
{"a"=>100, "d"=>467}
{"a"=>100, "c"=>300, "b"=>200, "g"=>647}