我创建一个哈希: a = {}= {} 然后: a.store(:b, {})= {} 和: a.merge!(c: {})= {:b={}, :c={}} 实际上有什么不同? store是一种分配方法. a = {}# = {}a.store(:b, {})a# = {:b={}}# Here you are assigning a key :b with e
a = {} => {}
然后:
a.store(:b, {}) => {}
和:
a.merge!(c: {}) => {:b=>{}, :c=>{}}
实际上有什么不同?
store是一种分配方法.a = {} # => {} a.store(:b, {}) a # => {:b=>{}} # Here you are assigning a key :b with empty hash {}
另一个让它更清晰的例子:
a = {} # => {} a.store("key", "value") a # => {"key"=>"value"}
另一方面,merge通过与不同的哈希合并来操纵您现有的哈希.
例:
a = {} # => {} a.merge({"key" => "value"}) # => {"key"=>"value"} a # => {} # original value still unchanged a.merge!({"key" => "value"}) # => {"key"=>"value"} a # => {"key"=>"value"} # original value updated
但是除非你使用合并! a的值不会改变,即仅在返回时才会发生合并.