https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112012-06-25T19:33:49ZRuby Issue Tracking SystemRuby master - Feature #6641: Hash.auto constructorhttps://bugs.ruby-lang.org/issues/6641?journal_id=274072012-06-25T19:33:49ZEregon (Benoit Daloze)
<ul></ul><p>trans (Thomas Sawyer) wrote:</p>
<blockquote>
<p>It is not uncommon to need a Hash following the pattern:</p>
<p>Hash.new{ |h,k| h[k]=[] }</p>
<p>This is common enough that it would very nice if we could have a more concise form, e.g.</p>
<p>Hash.auto{ [] }</p>
</blockquote>
<p>I agree, this is a pattern I see quite often and the <code>Hash.new { |h,k| h[k] = ... }</code> form is not the most natural or expressive in my opinion.<br>
But at the same time, this is clearly a specialization of Hash.new with a block, which loses some flexibility (you can not use the key for example). I'm curious what others think of it.</p> Ruby master - Feature #6641: Hash.auto constructorhttps://bugs.ruby-lang.org/issues/6641?journal_id=274082012-06-25T21:24:36Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>Maybe something like Hash.with_default{[]}. "with_default" could be both a class and an instance method so that you could have something like {a: 1, b: 2}.with_default(0).merge... It would always return the hash itself...</p>
<p>Groovy has something like this (not quite the same behavior). In Groovy you can define default values as [:].withDefault{0}. You can optionally use the block argument as the key.</p>
<p>So I guess it would be great if Ruby with_default supported all those syntax below:</p>
<p>Hash.with_default(0)<br>
Hash.with_default{Time.now}<br>
Hash.with_default{|k| k.to_sym}<br>
{}.with_default(0)<br>
{}.with_default{Time.now}<br>
{}.with_default{|k| k.to_sym}</p> Ruby master - Feature #6641: Hash.auto constructorhttps://bugs.ruby-lang.org/issues/6641?journal_id=274092012-06-25T21:29:10Zjwille (Jens Wille)ww@blackwinter.de
<ul></ul><p>Eregon (Benoit Daloze) [2012-06-25 12:33]:</p>
<blockquote>
<p>But at the same time, this is clearly a specialization of<br>
Hash.new with a block, which loses some flexibility (you can not<br>
use the key for example).<br>
why not? you can yield the key to the block.</p>
</blockquote>
<blockquote>
<p>I'm curious what others think of it.<br>
i like it and i use it occasionally:</p>
</blockquote>
<p><a href="http://blackwinter.github.com/ruby-nuggets/Nuggets/Hash/NestMixin.html" class="external">http://blackwinter.github.com/ruby-nuggets/Nuggets/Hash/NestMixin.html</a><br>
<a href="https://github.com/blackwinter/ruby-nuggets/blob/master/lib/nuggets/hash/nest_mixin.rb" class="external">https://github.com/blackwinter/ruby-nuggets/blob/master/lib/nuggets/hash/nest_mixin.rb</a><br>
<a href="https://github.com/blackwinter/ruby-nuggets/blob/master/spec/nuggets/hash/nest_spec.rb" class="external">https://github.com/blackwinter/ruby-nuggets/blob/master/spec/nuggets/hash/nest_spec.rb</a></p> Ruby master - Feature #6641: Hash.auto constructorhttps://bugs.ruby-lang.org/issues/6641?journal_id=274662012-06-26T12:19:45Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>-1.<br>
I don't like the destructive default proc, which sets new keys automatically.</p> Ruby master - Feature #6641: Hash.auto constructorhttps://bugs.ruby-lang.org/issues/6641?journal_id=274692012-06-26T14:23:22Zkosaki (Motohiro KOSAKI)kosaki.motohiro@gmail.com
<ul></ul><blockquote>
<p>Hash.with_default(0)<br>
Hash.with_default{Time.now}<br>
Hash.with_default{|k| k.to_sym}<br>
{}.with_default(0)<br>
{}.with_default{Time.now}<br>
{}.with_default{|k| k.to_sym}</p>
</blockquote>
<p>I like with_default than auto. It describe the behavior more preciously.<br>
auto is too vague to me.</p> Ruby master - Feature #6641: Hash.auto constructorhttps://bugs.ruby-lang.org/issues/6641?journal_id=315352012-10-25T19:38:45Zyhara (Yutaka HARA)
<ul><li><strong>Target version</strong> changed from <i>2.0.0</i> to <i>2.6</i></li></ul> Ruby master - Feature #6641: Hash.auto constructorhttps://bugs.ruby-lang.org/issues/6641?journal_id=359512013-02-07T08:21:43Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>I agree with nobu-shi, if I were to encounter <code>Hash::with_default &block</code> I would assume it was the equivalent of:</p>
<pre><code>Hash.new{ |h,k| yield }
</code></pre>
<p>rather than:</p>
<pre><code>Hash.new{ |h,k| h[k] = yield }
</code></pre>
<p>Perhaps there could be ::with_default and ::with_default!</p> Ruby master - Feature #6641: Hash.auto constructorhttps://bugs.ruby-lang.org/issues/6641?journal_id=367382013-02-22T09:08:58Zko1 (Koichi Sasada)
<ul><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li></ul> Ruby master - Feature #6641: Hash.auto constructorhttps://bugs.ruby-lang.org/issues/6641?journal_id=687942017-12-25T18:15:06Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Target version</strong> deleted (<del><i>2.6</i></del>)</li></ul>