title: How do I add a key/value pair to the beginning of a hash?
body: <p>My code is:</p>
  
  <pre><code>hash = { two: 2, three: 3 }
  
  def hash_add(hash, new_key, new_value)
    temp_hash = {}
  
    temp_hash[new_key.to_sym] = new_value
    temp_hash.merge!(hash)
    hash = temp_hash
    puts hash
  
  end
  
  hash_add(hash, 'one', 1)
  </code></pre>
  
  <p>Within the method, <code>puts hash</code> returns <code>{ :one =&gt; 1, :two =&gt; 2, :three =&gt; 3 }</code>, but when <code>hash1</code> is put to the method, it remains unchanged afterward.  It's like the assignment isn't carrying itself outside of the function.</p>
  
  <p>I guess I could return the updated hash and set the hash I want to change to it outside the method:</p>
  
  <pre><code>hash = hash_add(hash, 'one', 1)
  </code></pre>
  
  <p>But I just don't see why the assignment I give to the hash does not stick outside of the method.</p>
  
  <p>I have this, which works: </p>
  
  <pre><code>def hash_add(hash, new_key, new_value)
    temp_hash = {}
  
    temp_hash[new_key.to_sym] = new_value
    temp_hash.merge!(hash)
    hash.clear
  
    temp_hash.each do |key, value|
      hash[key] = value
    end
  end
  </code></pre>
  
  <p>Which gives me what I'm wanting when this method is called, but it just seems a little excessive to have to rebuild the hash like that.</p>