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=-2.8 required=3.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=no 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 E36DC1F463 for ; Thu, 26 Sep 2019 21:49:13 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 91E2B120AAF; Fri, 27 Sep 2019 06:49:05 +0900 (JST) Received: from xtrwkhkc.outbound-mail.sendgrid.net (xtrwkhkc.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id A0105120A8E for ; Fri, 27 Sep 2019 06:49:03 +0900 (JST) Received: by filter0162p3mdw1.sendgrid.net with SMTP id filter0162p3mdw1-2948-5D8D3252-43 2019-09-26 21:49:06.434419765 +0000 UTC m=+264468.077928234 Received: from herokuapp.com (unknown [34.207.207.12]) by ismtpd0066p1iad2.sendgrid.net (SG) with ESMTP id ic4fOMSWSLu8iw7craUhkA for ; Thu, 26 Sep 2019 21:49:06.338 +0000 (UTC) Date: Thu, 26 Sep 2019 21:49:06 +0000 (UTC) From: eregontp@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 70675 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16183 X-Redmine-Issue-Author: zverok X-Redmine-Sender: Eregon 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?KippOI8ZHtTweq7XfQzW93937kJ4QNWwSBuHnaMEcr25flHiigkDK1RoOLaow2?= =?us-ascii?Q?qUM5zvl=2Fdd1GbPHyr2VPPV3x2g5E+ExWkneFNfV?= =?us-ascii?Q?axTkf0CH0symDvKy6J80KhSHBN7rO8nD7KX=2FtxO?= =?us-ascii?Q?TG+49uPfUcN3xm7ZOu9fAR8P7oVxvvQTSdmfnvY?= =?us-ascii?Q?uoKcQRFWaX1b7rGq1I+RcTW2hfuWMobfnPw=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 95120 Subject: [ruby-core:95120] [Ruby master Feature#16183] Hash#with_default 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 #16183 has been updated by Eregon (Benoit Daloze). zverok (Victor Shepelev) wrote: > So, I propose side-effect-less method, that **is** acceptable for chaining, I believe. That means a copy of the Hash is necessary on each call to #with_default. I.e., it would be the same as: ```ruby h = {a: 1, b: 2} h2 = h.dup.tap { _1.default = 3 } ``` I think it's better to keep a potentially expensive copy of the Hash explicit with the `.dup` call + `default=`. But I agree there is some beauty to change the default value in a safe, non-mutable way with a single method call. ---------------------------------------- Feature #16183: Hash#with_default https://bugs.ruby-lang.org/issues/16183#change-81754 * Author: zverok (Victor Shepelev) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- Reasons: there is no way, currently, to *declaratively* define Hash with default value (for example, to store it in constant, or use in an expression). Which leads to code more or less like this: ```ruby FONTS = { title: 'Arial', body: 'Times New Roman', blockquote: 'Tahoma' }.tap { |h| h.default = 'Courier' }.freeze # Grouping indexes: ary.each_with_object(Hash.new { |h, k| h[k] = [] }).with_index { |(el, h), idx| h[el.downcase] << idx } ``` With proposed method: ```ruby FONTS = { title: 'Arial', body: 'Times New Roman', blockquote: 'Tahoma' }.with_default('Courier').freeze ary.each_with_object({}.with_default { [] }).with_index { |(el, h), idx| h[el.downcase] << idx } ``` About the block synopsys: I am not 100% sure, but I believe that _most_ of the time when `default_proc` provided, it looks like `{ |h, k| h[k] = some_calculation }`. So, I believe for this "declarative simplification" of defaults, it is acceptable to assume it as the only behavior (pass only key to block, and always store block's result); more flexible form would still be accessible with `Hash.new`. -- https://bugs.ruby-lang.org/