From: briank@kappacs.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:93743] [Ruby master Feature#11747] "bury" feature, similar to 'dig' but opposite
Date: Sun, 14 Jul 2019 02:52:56 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-79380.20190714025256.3126caebfd3a9b45@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-11747.20151127201637@ruby-lang.org
Issue #11747 has been updated by bkatzung (Brian Katzung).
Much of this has been available through my XKeys gem since Q2 2014.
```
data = {}.extend XKeys::Auto # Vs ::Hash, uses arrays for int keys
data[:users, 0, :name] # nil
data[:users, 0, :name, :raise => true] # KeyError
data[:users, :[], :name] = 'Matz' # :[] is next index, 0 in this case
# {:user=>[{:name=>"Matz"}]}
pick = [:users, 0, :name]
data[*pick] # Matz
data[:users, 0, :accesses, :else => 0] += 1
# {:user=>[{:name=>"Matz", :accesses=>1}]}
```
----------------------------------------
Feature #11747: "bury" feature, similar to 'dig' but opposite
https://bugs.ruby-lang.org/issues/11747#change-79380
* Author: dam13n (damien sutevski)
* Status: Rejected
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version:
----------------------------------------
In Matz's recent Rubyconf talk, he used this example for the new 'dig' feature coming in Ruby 2.3:
~~~ruby
# we want this
data[:users][0][:name]
# we can do this w/o nil errors
data.dig(:users, 0, :name)
~~~
What I'm proposing is a 'bury' feature that is the opposite of 'dig' in a sense. It inserts a value at an arbitrary depth, for example:
~~~ruby
data.bury(:users, 0, :name, 'Matz')
~~~
This will create a nested hash or an array automatically at each step if it doesn't already exist, and that can be inferred from the what the user is passing (such as a symbol or string for a hash or an integer for an array). It's similar to autovivification but more powerful!
This behavior is very common, at least in my experience, so a dry method built into Ruby would be awesome!
---Files--------------------------------
bury_examples.rb (1 KB)
--
https://bugs.ruby-lang.org/
next prev parent reply other threads:[~2019-07-14 2:53 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-11747.20151127201637@ruby-lang.org>
2015-11-27 20:16 ` [ruby-core:71709] [Ruby trunk - Feature #11747] [Open] "bury" feature, similar to 'dig' but opposite dameyawn
2015-11-27 22:33 ` [ruby-core:71714] [Ruby trunk - Feature #11747] [Feedback] " nobu
2015-11-28 14:38 ` [ruby-core:71720] [Ruby trunk - Feature #11747] " sawadatsuyoshi
2015-12-02 23:36 ` [ruby-core:71805] " dameyawn
2015-12-07 7:29 ` [ruby-core:71885] [Ruby trunk - Feature #11747] [Rejected] " matz
2018-05-17 16:42 ` [ruby-core:87143] [Ruby trunk Feature#11747] " brianhingyenkung
2019-07-14 2:52 ` briank [this message]
2020-01-11 20:38 ` [ruby-core:96790] [Ruby master " from-ruby-lang
2020-01-11 20:44 ` [ruby-core:96791] " from-ruby-lang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-79380.20190714025256.3126caebfd3a9b45@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).