Ruby Issue Tracking System: Issueshttps://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112014-06-11T00:08:12ZRuby Issue Tracking System
Redmine Ruby master - Feature #9929 (Open): add with_default method to Hashhttps://redmine.ruby-lang.org/issues/99292014-06-11T00:08:12Zdontfidget (Andrew Brown)andrew@dontfidget.com
<p>I would like to be able to write this:</p>
<pre><code>people.group_by(:first_name).with_default { [] }
</code></pre>
<p>instead of:</p>
<pre><code>hash = people.group_by(:first_name)
hash.default_proc = proc { [] }
</code></pre>
<p>or, perhaps:</p>
<pre><code>people.group_by(:first_name).tap { |hash| hash.default_proc = proc { [] } }
</code></pre>
<p>Just wondering if anyone else would be interested in a change like this. I posted a pull request in Rails for an ActiveSupport core extension that does the same thing but would love to see it in Ruby proper. (Pull request is at <a href="https://github.com/rails/rails/pull/15626" class="external">https://github.com/rails/rails/pull/15626</a>)</p>