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 B176B1F466 for ; Thu, 26 Sep 2019 13:01:38 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id EFF11120B78; Thu, 26 Sep 2019 22:01:29 +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 D86A9120B16 for ; Thu, 26 Sep 2019 22:01:26 +0900 (JST) Received: by filter0150p3mdw1.sendgrid.net with SMTP id filter0150p3mdw1-1152-5D8CB6A3-9F 2019-09-26 13:01:23.595619229 +0000 UTC m=+231667.623927782 Received: from herokuapp.com (unknown [34.207.207.12]) by ismtpd0053p1iad2.sendgrid.net (SG) with ESMTP id oQCVMC9ATfSVEscsc7STmQ for ; Thu, 26 Sep 2019 13:01:23.512 +0000 (UTC) Date: Thu, 26 Sep 2019 13:01:28 +0000 (UTC) From: zverok.offline@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 70659 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16183 X-Redmine-Issue-Author: zverok X-Redmine-Sender: zverok 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?3be0g8093pjUjT94eiCA64csFDBI=2FmHQTWm54P5gda6uZAcZJ0y7ZYd7+chZdW?= =?us-ascii?Q?WYb2=2FbExPjhNms8k9+ySrWV5m7b0TErmjQOPDXE?= =?us-ascii?Q?GgzzEa5PbagxPhNftgQO62MQ3NcZMWBMb4el=2FWl?= =?us-ascii?Q?9iWQXU+pchTfqcIjx6gykmpmqj2c3ciqriivr3v?= =?us-ascii?Q?vV4m0WWtsAsS73cfd+tfD2rwmjVhQZcrZFQ=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 95104 Subject: [ruby-core:95104] [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 reported by zverok (Victor Shepelev). ---------------------------------------- Feature #16183: Hash#with_default https://bugs.ruby-lang.org/issues/16183 * 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/