title: Ruby class initialize method with default hash
body: <p>I stumbled across this functionality in Sandi Metz's presentation <a href="https://youtu.be/OMPfEXIlTVE?t=1819" rel="nofollow">here</a>.</p>
  
  <p>It seems that you can define an class's <code>initialize</code> function to accept a hash (without a name), and that has is automatically converted to arguments with the key's as names? Is that true? Where is this defined? Here's an example:</p>
  
  <pre><code>class House
    attr_reader :var1
    attr_reader :var2
  
    def initialize(var1: 'var1value', var2: 'var2value')
      puts var1
      puts var2
    end
  end
  
  House.new
  </code></pre>
  
  <p>Running this file will output:</p>
  
  <pre><code>var1value
  var2value
  </code></pre>
  
  <p>Additionally, it looks like the default hash is merged with values being passed in. For example:</p>
  
  <pre><code>class House
    attr_reader :var1
    attr_reader :var2
  
    def initialize(var1: 'var1value', var2: 'var2value')
      puts var1
      puts var2
    end
  end
  
  House.new(var2: 'newvar2value')
  </code></pre>
  
  <p>Running this will output:</p>
  
  <pre><code>var1value
  newvar2value
  </code></pre>
  
  <p>This seems like really awesome and useful functionality. I never realized this could be done. Is it documented somewhere? Or can someone explain what is happening here?</p>