From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-3.9 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 4D9971F461 for ; Sun, 14 Jul 2019 02:53:04 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 4E7A71209DC; Sun, 14 Jul 2019 11:52:57 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id BD3681209DA for ; Sun, 14 Jul 2019 11:52:54 +0900 (JST) Received: by filter0098p3iad2.sendgrid.net with SMTP id filter0098p3iad2-8088-5D2A9908-10 2019-07-14 02:52:56.450470943 +0000 UTC m=+891263.130245460 Received: from herokuapp.com (unknown [54.166.109.56]) by ismtpd0008p1iad2.sendgrid.net (SG) with ESMTP id ecXx6cgmS_u9utwhJDTReQ for ; Sun, 14 Jul 2019 02:52:56.437 +0000 (UTC) Date: Sun, 14 Jul 2019 02:52:56 +0000 (UTC) From: briank@kappacs.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 69239 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 11747 X-Redmine-Issue-Author: dam13n X-Redmine-Issue-Assignee: matz X-Redmine-Sender: bkatzung X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?PqdxeVP4Sf5Up5EGxI7+NYeIWuWnZHR0zeIHosWLMyx3C9KoURvmh1KdzwnG9l?= =?us-ascii?Q?K+KjYqs=2F=2FBN83SNHDlMU1QWKfqN4zvJFr1W3HWe?= =?us-ascii?Q?wp4YJ28i+q5tfRPgYFf+gkPok1jygchUv+N1Ouy?= =?us-ascii?Q?PAyhfTk9q9+kz3RK1d5rHiJEnUx5ucovm+h3OMx?= =?us-ascii?Q?LxTFkaQT3Ioj3IhdfmlYeJrrGiOAOUOXNuA=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 93743 Subject: [ruby-core:93743] [Ruby master Feature#11747] "bury" feature, similar to 'dig' but opposite X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" 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/