From: "mame (Yusuke Endoh)" Date: 2012-11-24T08:45:17+09:00 Subject: [ruby-core:49914] [ruby-trunk - Feature #5531] deep_value for dealing with nested hashes Issue #5531 has been updated by mame (Yusuke Endoh). Priority changed from Normal to Low Target version set to next minor matz expressed a negative opinion for similar proposal (in Japanese, #5550) The original in Japanese: > Hash������������key-value������������������������������value���������������Hash������������������������������(������������Hash���������������������������������)��������������������������������������������������������������� English translation: > 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. -- Yusuke Endoh ---------------------------------------- Feature #5531: deep_value for dealing with nested hashes https://bugs.ruby-lang.org/issues/5531#change-33673 Author: weexpectedTHIS (Kyle Peyton) Status: Assigned Priority: Low Assignee: matz (Yukihiro Matsumoto) Category: Target version: next minor This feature request stems from dealing with nested hashes, like the params from a request often dealt with in web frameworks. Conditional code often needs to be written with multiple logical ANDs in order to achieve what this simple function can: class Hash def deep_value(*ks) if ks.size == 1 return self[ks.shift] else val = ks.shift return (self[val].is_a?(Hash) ? self[val].deep_value(*ks) : nil) end end alias dv deep_value end deep_value (dv) will simply recurse over a hash given a set of indexes and return the value at the end. Example: > foo = {:bar => {:baz => 'blah'}} > foo.dv(:bar, :baz) -> 'blah' > foo.dv(:cats) -> nil -- http://bugs.ruby-lang.org/