有两种方法可以为 a 创建初始值Hash
.
一种是将单个对象传递给Hash.new
。这在许多情况下都很有效,特别是当对象是冻结值时,但如果对象具有内部状态,这可能会产生意想不到的副作用。由于同一对象在之间共享all没有指定值的键,修改其中一个的内部状态将全部显示。
a_hash = Hash.new "initial value"
a_hash['a'] #=> "initial value"
# op= methods don't modify internal state (usually), since they assign a new
# value for the key.
a_hash['b'] += ' owned by b' #=> "initial value owned by b"
# other methods, like #<< and #gsub modify the state of the string
a_hash['c'].gsub!(/initial/, "c's")
a_hash['d'] << " modified by d"
a_hash['e'] #=> "c's value modified by d"
另一种初始化方法是通过Hash.new
一个块,每次为没有值的键请求值时都会调用该块。这允许您为每个键使用不同的值。
another_hash = Hash.new { "new initial value" }
another_hash['a'] #=> "new initial value"
# op= methods still work as expected
another_hash['b'] += ' owned by b'
# however, if you don't assign the modified value, it's lost,
# since the hash rechecks the block every time an unassigned key's value is asked for
another_hash['c'] << " owned by c" #=> "new initial value owned by c"
another_hash['c'] #=> "new initial value"
该块传递两个参数:请求值的哈希值和使用的密钥。这使您可以选择为该键分配一个值,以便每次给出特定键时都会显示相同的对象。
yet_another_hash = Hash.new { |hash, key| hash[key] = "#{key}'s initial value" }
yet_another_hash['a'] #=> "a's initial value"
yet_another_hash['b'] #=> "b's initial value"
yet_another_hash['c'].gsub!('initial', 'awesome')
yet_another_hash['c'] #=> "c's awesome value"
yet_another_hash #=> { "a" => "a's initial value", "b" => "b's initial value", "c" => "c's awesome value" }
最后一种方法是我最常使用的方法。它对于缓存昂贵的计算结果也很有用。