https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112011-11-01T09:29:10ZRuby Issue Tracking SystemRuby master - Feature #5531: deep_value for dealing with nested hasheshttps://bugs.ruby-lang.org/issues/5531?journal_id=217342011-11-01T09:29:10Zko1 (Koichi Sasada)
<ul></ul><p>(2011/11/01 8:52), Kyle Peyton wrote:</p>
<blockquote>
<p>Example:</p>
<blockquote>
<p>foo = {:bar => {:baz => 'blah'}}<br>
foo.dv(:bar, :baz)<br>
-> 'blah'<br>
foo.dv(:cats)<br>
-> nil</p>
</blockquote>
</blockquote>
<p>Just idea.<br>
How about to extend Hash#[] for it?</p>
<p>--<br>
// SASADA Koichi at atdot dot net</p> Ruby master - Feature #5531: deep_value for dealing with nested hasheshttps://bugs.ruby-lang.org/issues/5531?journal_id=217372011-11-01T09:53:08Zrkh (Konstantin Haase)me@rkh.im
<ul></ul><p>What's the difference (usability wise) between</p>
<pre><code>hash[:foo][:bar]
</code></pre>
<p>and</p>
<pre><code>hash.dv(:foo, :bar)
</code></pre>
<p>Konstantin</p>
<p>On Oct 31, 2011, at 16:52 , Kyle Peyton wrote:</p>
<blockquote>
<p>Issue <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: deep_value for dealing with nested hashes (Closed)" href="https://bugs.ruby-lang.org/issues/5531">#5531</a> has been reported by Kyle Peyton.</p>
<hr>
<p>Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: deep_value for dealing with nested hashes (Closed)" href="https://bugs.ruby-lang.org/issues/5531">#5531</a>: deep_value for dealing with nested hashes<br>
<a href="http://redmine.ruby-lang.org/issues/5531" class="external">http://redmine.ruby-lang.org/issues/5531</a></p>
<p>Author: Kyle Peyton<br>
Status: Open<br>
Priority: Normal<br>
Assignee:<br>
Category:<br>
Target version:</p>
<p>This feature request stems from dealing with nested hashes, like the params from a request often dealt with in web frameworks.</p>
<p>Conditional code often needs to be written with multiple logical ANDs in order to achieve what this simple function can:</p>
<p>class Hash<br>
def deep_value(*ks)<br>
if ks.size</p>
</blockquote> Ruby master - Feature #5531: deep_value for dealing with nested hasheshttps://bugs.ruby-lang.org/issues/5531?journal_id=217382011-11-01T09:53:09Zrkh (Konstantin Haase)me@rkh.im
<ul></ul><p>What's the difference (usability wise) between</p>
<pre><code>hash[:foo][:bar]
</code></pre>
<p>and</p>
<pre><code>hash.dv(:foo, :bar)
</code></pre>
<p>Konstantin</p>
<p>On Oct 31, 2011, at 16:52 , Kyle Peyton wrote:</p>
<blockquote>
<p>Issue <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: deep_value for dealing with nested hashes (Closed)" href="https://bugs.ruby-lang.org/issues/5531">#5531</a> has been reported by Kyle Peyton.</p>
<hr>
<p>Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: deep_value for dealing with nested hashes (Closed)" href="https://bugs.ruby-lang.org/issues/5531">#5531</a>: deep_value for dealing with nested hashes<br>
<a href="http://redmine.ruby-lang.org/issues/5531" class="external">http://redmine.ruby-lang.org/issues/5531</a></p>
<p>Author: Kyle Peyton<br>
Status: Open<br>
Priority: Normal<br>
Assignee:<br>
Category:<br>
Target version:</p>
<p>This feature request stems from dealing with nested hashes, like the params from a request often dealt with in web frameworks.</p>
<p>Conditional code often needs to be written with multiple logical ANDs in order to achieve what this simple function can:</p>
<p>class Hash<br>
def deep_value(*ks)<br>
if ks.size</p>
</blockquote> Ruby master - Feature #5531: deep_value for dealing with nested hasheshttps://bugs.ruby-lang.org/issues/5531?journal_id=217392011-11-01T09:53:09Zrkh (Konstantin Haase)me@rkh.im
<ul></ul><p>Never mind, got it.</p>
<p>On Oct 31, 2011, at 17:32 , Haase, Konstantin wrote:</p>
<blockquote>
<p>What's the difference (usability wise) between</p>
<p>hash[:foo][:bar]</p>
<p>and</p>
<p>hash.dv(:foo, :bar)</p>
<p>Konstantin</p>
<p>On Oct 31, 2011, at 16:52 , Kyle Peyton wrote:</p>
<blockquote>
<p>Issue <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: deep_value for dealing with nested hashes (Closed)" href="https://bugs.ruby-lang.org/issues/5531">#5531</a> has been reported by Kyle Peyton.</p>
<hr>
<p>Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: deep_value for dealing with nested hashes (Closed)" href="https://bugs.ruby-lang.org/issues/5531">#5531</a>: deep_value for dealing with nested hashes<br>
<a href="http://redmine.ruby-lang.org/issues/5531" class="external">http://redmine.ruby-lang.org/issues/5531</a></p>
<p>Author: Kyle Peyton<br>
Status: Open<br>
Priority: Normal<br>
Assignee:<br>
Category:<br>
Target version:</p>
<p>This feature request stems from dealing with nested hashes, like the params from a request often dealt with in web frameworks.</p>
<p>Conditional code often needs to be written with multiple logical ANDs in order to achieve what this simple function can:</p>
<p>class Hash<br>
def deep_value(*ks)<br>
if ks.size</p>
</blockquote>
</blockquote> Ruby master - Feature #5531: deep_value for dealing with nested hasheshttps://bugs.ruby-lang.org/issues/5531?journal_id=217722011-11-01T20:23:18ZEregon (Benoit Daloze)
<ul></ul><p>On 1 November 2011 01:26, SASADA Koichi <a href="mailto:ko1@atdot.net" class="email">ko1@atdot.net</a> wrote:</p>
<blockquote>
<p>(2011/11/01 8:52), Kyle Peyton wrote:</p>
<blockquote>
<p>Example:</p>
<blockquote>
<p>foo = {:bar => {:baz => 'blah'}}<br>
foo.dv(:bar, :baz)<br>
-> 'blah'<br>
foo.dv(:cats)<br>
-> nil</p>
</blockquote>
</blockquote>
<p>Just idea.<br>
How about to extend Hash#[] for it?</p>
<p>--<br>
// SASADA Koichi at atdot dot net</p>
</blockquote>
<p>That would be nice.</p> Ruby master - Feature #5531: deep_value for dealing with nested hasheshttps://bugs.ruby-lang.org/issues/5531?journal_id=217732011-11-01T20:34:20Zalexeymuranov (Alexey Muranov)
<ul></ul><p>Konstantin Haase wrote:</p>
<blockquote>
<p>Never mind, got it.</p>
<p>On Oct 31, 2011, at 17:32 , Haase, Konstantin wrote:</p>
<blockquote>
<p>What's the difference (usability wise) between</p>
<p>hash[:foo][:bar]</p>
<p>and</p>
<p>hash.dv(:foo, :bar)</p>
<p>Konstantin</p>
</blockquote>
</blockquote>
<p>I'll answer anyway if someone else didn't get it :).<br>
{ :foo => 1 }[2][3] raises NoMethodError, and { :foo => 1 }.dv(2,3) or { :foo => 1 }[2,3] should return nil.</p>
<p>Update: also it is possible to keep the list of all arguments in a single variable and call { :foo => 1 }.dv(*args)</p> Ruby master - Feature #5531: deep_value for dealing with nested hasheshttps://bugs.ruby-lang.org/issues/5531?journal_id=217802011-11-01T21:45:16Zalexeymuranov (Alexey Muranov)
<ul></ul><p>I can think of the following questions/objections to the suggested method definition:</p>
<ol>
<li>is <code>{ 1 => 2}.dv(1,1) # => nil</code> the desired result?</li>
<li>this method examines the (super)class name of an object, rather than the behavior of an object, so does not allow to mix nested hashes and arrays,</li>
<li>this method calls itself recursively, while a loop would suffice.</li>
</ol>
<p>The following is not a serious suggestion, but seriously, how about:</p>
<p>class Object<br>
def deep_value(*keys)<br>
obj = self<br>
obj = obj[keys.shift] while !keys.empty? && obj.respond_to?(:[])<br>
return obj<br>
end<br>
end</p>
<p>(For this to work well it will be important to call it #deep_value and not to redefine #[].)</p>
<hr>
<p>Update. Another suggestion, probably a better one (at least simpler):</p>
<p>class Object<br>
def deep_value(*keys)<br>
obj = self<br>
obj = obj[keys.shift] until keys.empty? || obj.nil?<br>
return obj<br>
end<br>
end</p> Ruby master - Feature #5531: deep_value for dealing with nested hasheshttps://bugs.ruby-lang.org/issues/5531?journal_id=217852011-11-01T22:33:06Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>=begin<br>
What about:</p>
<p>class Hash<br>
def <a href="*keys"></a><br>
keys.inject(self) {|container, key| container.fetch(key) {return}}<br>
end<br>
end<br>
=end</p> Ruby master - Feature #5531: deep_value for dealing with nested hasheshttps://bugs.ruby-lang.org/issues/5531?journal_id=217862011-11-01T22:42:43Zalexeymuranov (Alexey Muranov)
<ul></ul><p>Nobuyoshi Nakada wrote:</p>
<blockquote>
<p>=begin<br>
What about:</p>
<p>class Hash<br>
def <a href="*keys"></a><br>
keys.inject(self) {|container, key| container.fetch(key) {return}}<br>
end<br>
end<br>
=end</p>
</blockquote>
<p>Just a small remark about defining this exclusively for Hash: what if some of the values is not a Hash but responds to #fetch? (will not look consistent enough to me).</p> Ruby master - Feature #5531: deep_value for dealing with nested hasheshttps://bugs.ruby-lang.org/issues/5531?journal_id=219152011-11-06T17:48:51Ztrans (Thomas Sawyer)
<ul></ul><p>Probably best to use #[] internally too.</p>
<pre><code>class Hash
def [](*keys)
keys.inject(self) {|container, key| value = container[key]; value ? value : return value}
end
end
</code></pre>
<p>@Alexey you may have a point. But I suspect it would need to be conditioned off of responding to #to_h or #to_hash instead of using <code>is_a?(Hash)</code>.</p> Ruby master - Feature #5531: deep_value for dealing with nested hasheshttps://bugs.ruby-lang.org/issues/5531?journal_id=219372011-11-07T01:23:17Zneleai (Ondrej Bilka)neleai@seznam.cz
<ul></ul><p>Do you need hash or something like multidimensional hash class that uses [], each iterates on nested...<br>
On Sun, Nov 06, 2011 at 05:48:52PM +0900, Thomas Sawyer wrote:</p>
<blockquote>
<p>Issue <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: deep_value for dealing with nested hashes (Closed)" href="https://bugs.ruby-lang.org/issues/5531">#5531</a> has been updated by Thomas Sawyer.</p>
<p>Probably best to use #[] internally too.</p>
<pre><code>class Hash
def [](*keys)
keys.inject(self) {|container, key| value = container[key]; value ? value : return value}
end
end
</code></pre>
<p>@Alexey you may have a point. But I suspect it would need to be conditioned off of responding to #to_h or #to_hash instead of using <code>is_a?(Hash)</code>.</p>
<hr>
<p>Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: deep_value for dealing with nested hashes (Closed)" href="https://bugs.ruby-lang.org/issues/5531">#5531</a>: deep_value for dealing with nested hashes<br>
<a href="http://redmine.ruby-lang.org/issues/5531" class="external">http://redmine.ruby-lang.org/issues/5531</a></p>
<p>Author: Kyle Peyton<br>
Status: Open<br>
Priority: Normal<br>
Assignee:<br>
Category:<br>
Target version:</p>
<p>This feature request stems from dealing with nested hashes, like the params from a request often dealt with in web frameworks.</p>
<p>Conditional code often needs to be written with multiple logical ANDs in order to achieve what this simple function can:</p>
<p>class Hash<br>
def deep_value(*ks)<br>
if ks.size == 1<br>
return self[ks.shift]<br>
else<br>
val = ks.shift<br>
return (self[val].is_a?(Hash) ? self[val].deep_value(*ks) : nil)<br>
end<br>
end</p>
<p>alias dv deep_value<br>
end</p>
<p>deep_value (dv) will simply recurse over a hash given a set of indexes and return the value at the end.</p>
<p>Example:</p>
<blockquote>
<p>foo = {:bar => {:baz => 'blah'}}<br>
foo.dv(:bar, :baz)<br>
-> 'blah'<br>
foo.dv(:cats)<br>
-> nil</p>
</blockquote>
<p>--<br>
<a href="http://redmine.ruby-lang.org" class="external">http://redmine.ruby-lang.org</a></p>
</blockquote>
<p>--</p>
<p>network packets travelling uphill (use a carrier pigeon)</p> Ruby master - Feature #5531: deep_value for dealing with nested hasheshttps://bugs.ruby-lang.org/issues/5531?journal_id=252432012-03-27T22:51:23Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li></ul> Ruby master - Feature #5531: deep_value for dealing with nested hasheshttps://bugs.ruby-lang.org/issues/5531?journal_id=253902012-03-30T01:04:06ZweexpectedTHIS (Kyle Peyton)kylepeyton@gmail.com
<ul></ul><p>What's the status of this issue? Good idea? Bad idea?</p> Ruby master - Feature #5531: deep_value for dealing with nested hasheshttps://bugs.ruby-lang.org/issues/5531?journal_id=274912012-06-27T03:10:40ZweexpectedTHIS (Kyle Peyton)kylepeyton@gmail.com
<ul></ul><p>I'd really like to see this in the next version of Ruby, it's a really common pattern.</p> Ruby master - Feature #5531: deep_value for dealing with nested hasheshttps://bugs.ruby-lang.org/issues/5531?journal_id=299262012-10-02T07:12:15ZweexpectedTHIS (Kyle Peyton)kylepeyton@gmail.com
<ul></ul><p>I think there is a strong case for this logic built in to ruby.</p> Ruby master - Feature #5531: deep_value for dealing with nested hasheshttps://bugs.ruby-lang.org/issues/5531?journal_id=336732012-11-24T08:45:16Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Priority</strong> changed from <i>Normal</i> to <i>3</i></li><li><strong>Target version</strong> set to <i>2.6</i></li></ul><p>matz expressed a negative opinion for similar proposal (in Japanese, <a class="issue tracker-2 status-6 priority-4 priority-default closed" title="Feature: Hash#depth, Hash#flat_length for recursive hashes (Rejected)" href="https://bugs.ruby-lang.org/issues/5550">#5550</a>)</p>
<p>The original in Japanese:</p>
<blockquote>
<p>Hashの本質はkey-valueのマッピングなので、valueが再帰的にHashであることを想定した(再帰的なHashでなければ役に立たない)メソッドを追加することには抵抗があります。</p>
</blockquote>
<p>English translation:</p>
<blockquote>
<p>The essence of Hash is a key-value mapping. I'm negative for adding a method that assumes that the value is a recursive hash, or a method that is useful only for a recursive hash.</p>
</blockquote>
<p>--<br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp" class="email">mame@tsg.ne.jp</a></p> Ruby master - Feature #5531: deep_value for dealing with nested hasheshttps://bugs.ruby-lang.org/issues/5531?journal_id=607992016-10-08T14:21:25Zdan.erikson (Dan Erikson)
<ul></ul><p>I believe this has recently been implemented as <code>Hash#dig</code>.</p> Ruby master - Feature #5531: deep_value for dealing with nested hasheshttps://bugs.ruby-lang.org/issues/5531?journal_id=611332016-10-31T03:48:09Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Closed</i></li></ul><p>Dan Erikson wrote:</p>
<blockquote>
<p>I believe this has recently been implemented as <code>Hash#dig</code>.</p>
</blockquote>
<p>Indeed. Closing peacefully.</p>