Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are two ways to create initial values with for a <code>Hash</code>.</p> <p>One is to pass a single object in to <code>Hash.new</code>. 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 <em>all</em> keys without an assigned value, modifying the internal state for one will show up in all.</p> <pre><code>a_hash = Hash.new "initial value" a_hash['a'] #=&gt; "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' #=&gt; "initial value owned by b" # other methods, like #&lt;&lt; and #gsub modify the state of the string a_hash['c'].gsub!(/initial/, "c's") a_hash['d'] &lt;&lt; " modified by d" a_hash['e'] #=&gt; "c's value modified by d" </code></pre> <p>Another initialization method is to pass <code>Hash.new</code> 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. </p> <pre><code>another_hash = Hash.new { "new initial value" } another_hash['a'] #=&gt; "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'] &lt;&lt; " owned by c" #=&gt; "new initial value owned by c" another_hash['c'] #=&gt; "new initial value" </code></pre> <p>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.</p> <pre><code>yet_another_hash = Hash.new { |hash, key| hash[key] = "#{key}'s initial value" } yet_another_hash['a'] #=&gt; "a's initial value" yet_another_hash['b'] #=&gt; "b's initial value" yet_another_hash['c'].gsub!('initial', 'awesome') yet_another_hash['c'] #=&gt; "c's awesome value" yet_another_hash #=&gt; { "a" =&gt; "a's initial value", "b" =&gt; "b's initial value", "c" =&gt; "c's awesome value" } </code></pre> <p>This last method is the one I most often use. It's also useful for caching the result of an expensive calculation.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload