From: robb+ruby@...
Date: 2018-12-28T05:31:38+00:00
Subject: [ruby-core:90768] [Ruby trunk Feature#12282] Hash#dig! for repeated applications of Hash#fetch
Issue #12282 has been updated by robb (Robb Shecter).
Another naming idea is `#fetch_all`, signalling that this is essentially a `#fetch` over a list of keys.
----------------------------------------
Feature #12282: Hash#dig! for repeated applications of Hash#fetch
https://bugs.ruby-lang.org/issues/12282#change-75940
* Author: robb (Robb Shecter)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
A new feature for your consideration: #dig! which is to #fetch as #dig is to #[]. For me and maybe many others, Hash#fetch is used much more than Hash#[]. And traversing multiple fetches isn't very convenient nor Ruby-like, e.g.: places.fetch(:countries).fetch(:canada).fetch(ontario).
Here's how it would work:
~~~ruby
places = { countries: { canada: true } }
places.dig :countries, :canada # => true
places.dig! :countries, :canada # => true
places.dig :countries, :canada, :ontario # => nil
places.dig! :countries, :canada, :ontario # => KeyError: Key not found: :ontario
~~~
Here's an implementation and tests: https://gist.github.com/dogweather/819ccdb41c9db0514c163cfdb1c528e2
--
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>