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.7 required=3.0 tests=AWL,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 A979F1F463 for ; Thu, 26 Sep 2019 16:37:01 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id CFC4E120997; Fri, 27 Sep 2019 01:36:52 +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 1DD04120997 for ; Fri, 27 Sep 2019 01:36:49 +0900 (JST) Received: by filter0163p3mdw1.sendgrid.net with SMTP id filter0163p3mdw1-16361-5D8CE926-C 2019-09-26 16:36:54.07232412 +0000 UTC m=+245294.994349010 Received: from herokuapp.com (unknown [34.207.207.12]) by ismtpd0004p1iad1.sendgrid.net (SG) with ESMTP id 6np_pABST4KA2JFXrFyQ5w for ; Thu, 26 Sep 2019 16:36:53.999 +0000 (UTC) Date: Thu, 26 Sep 2019 16:36:54 +0000 (UTC) From: shevegen@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 70670 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16183 X-Redmine-Issue-Author: zverok X-Redmine-Sender: shevegen 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?6lbdtOg4RDRLuxD00eQtQKgoNAsge5d4xND7cbMQd0zvpM9ImMY4QZ3if9kD=2F9?= =?us-ascii?Q?NMIg+mBNcsijw3b29wuI3KmGl9BwlBBWWef81=2F6?= =?us-ascii?Q?wJWEN+WY30F2cxomPkAkz3VXZ30ebyYW00jE4uz?= =?us-ascii?Q?HNRf+ZCpW4sZkcfccfeGJ9gGYjLrMzeYmw=2FDnqn?= =?us-ascii?Q?rweJ4DHdsgabQluvCeSFgM0bOrD3fCLQm5Q=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 95115 Subject: [ruby-core:95115] [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 shevegen (Robert A. Heiler). sawa has it all covered. ;) The explanation by matz is interesting. "Use tap. Methods with side-effect should be handled with care. Making it chainable has little benefit." Personally I think other names, be these default_set, `Hash#default_proc_set or with_default, do not completely correlate towards #tap, in my opinion; but I think the problem is of special methods that may each have special meaning in different parts of ruby. So from this point of view, unifying via .tap is simpler. On the other hand, "tap" itself, at the least to me, conveys a slightly different meaning than #with_default does, so I am not sure the two use cases overlap 100%. Not sure if this would warrant the addition of a new syntax + idiom, and I am not really pro/con either, so that could be discussed. The complexity of method chains should be considered too, though. This may be an individual's style, but these huge chains may impose a cognitive load to some ruby users possibly. ---------------------------------------- Feature #16183: Hash#with_default https://bugs.ruby-lang.org/issues/16183#change-81749 * 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/