初始化哈希 [英] Initializing hashes
问题描述
我经常写这样的内容:
a_hash ['x']? a_hash ['x'] + ='多一些文字':a_hash ['x'] ='第一文字'
应该有更好的方法来做到这一点,但我找不到它。
有两种方法可以用 Hash
创建初始值。
一种是将单个对象传递给 Hash.new
。这在很多情况下都能很好地工作,特别是如果对象是冻结的值,但是如果对象具有内部状态,这可能会产生意想不到的副作用。由于同一个对象在没有指定值的情况下在所有键之间共享,所以修改其中一个的内部状态会显示出来。
a_hash = Hash.new初始值
a_hash ['a']#=> 初始值
#op =方法不会修改内部状态(通常),因为它们为键分配了新的
#值。
a_hash ['b'] + ='拥有b'#=> b拥有的初始值
#其他方法,如#<<<<和#gsub修改字符串
a_hash ['c']。gsub!(/ initial /,c's)
a_hash ['d']的状态'<< 由d修改
a_hash ['e']#=> c的值由d修改
另一个初始化方法是传递 Hash。新的
块,每次为没有值的密钥请求一个值时调用该块。这允许您为每个键使用不同的值。
another_hash = Hash.new {new initial value}
another_hash ['a']#=> new initial value
#op =方法仍然按预期工作
another_hash ['b'] + ='拥有b'
#但是,如果您不指定修改值,它会丢失
#,因为每次未分配密钥的值被要求
another_hash ['c']<<< 由c拥有#=> c拥有的新初始值
another_hash ['c']#=> 新的初始值
该块传递了两个参数:散列被要求输入一个值,使用的钥匙。这使您可以为该键分配一个值,以便每次给定特定键时都会显示相同的对象。
yet_another_hash = Hash.new {| hash,key | hash [key] =#{key}的初始值}
yet_another_hash ['a']#=> a的初始值
yet_another_hash ['b']#=> b的初始值
yet_another_hash ['c'] .gsub!('initial','awesome')
yet_another_hash ['c']#=> c's awesome value
yet_another_hash#=> {a=> a的初始值,b=> b的初始值,c=> c's awesome value}
最后一种方法是我最常用的方法。它对缓存昂贵的计算结果也很有用。
I frequently write something like this:
a_hash['x'] ? a_hash['x'] += ' some more text' : a_hash['x'] = 'first text'
There ought to be a better way to do this, but I can't find it.
There are two ways to create initial values with for a Hash
.
One is to pass a single object in to Hash.new
. This works well in many situations, especially if the object is a frozen value, but if the object has internal state, this may have unexpected side-effects. Since the same object is shared between all keys without an assigned value, modifying the internal state for one will show up in 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"
Another initialization method is to pass Hash.new
a block, which is invoked each time a value is requested for a key that has no value. This allows you to use a distinct value for each key.
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"
The block is passed two arguments: the hash being asked for a value, and the key used. This gives you the option of assigning a value for that key, so that the same object will be presented each time a particular key is given.
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" }
This last method is the one I most often use. It's also useful for caching the result of an expensive calculation.
这篇关于初始化哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!