[#77789] [Ruby trunk Feature#12012] Add Boolean method — prodis@...
Issue #12012 has been updated by Fernando Hamasaki de Amorim.
4 messages
2016/10/27
[ruby-core:77827] [Ruby trunk Feature#5531][Closed] deep_value for dealing with nested hashes
From:
shyouhei@...
Date:
2016-10-31 03:48:10 UTC
List:
ruby-core #77827
Issue #5531 has been updated by Shyouhei Urabe.
Status changed from Assigned to Closed
Dan Erikson wrote:
> I believe this has recently been implemented as `Hash#dig`.
Indeed. Closing peacefully.
----------------------------------------
Feature #5531: deep_value for dealing with nested hashes
https://bugs.ruby-lang.org/issues/5531#change-61133
* Author: Kyle Peyton
* Status: Closed
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
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
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>