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 B101E1F463 for ; Thu, 26 Sep 2019 19:23:52 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 47BB61209C9; Fri, 27 Sep 2019 04:23:42 +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 7027A1209C9 for ; Fri, 27 Sep 2019 04:23:39 +0900 (JST) Received: by filter0187p3mdw1.sendgrid.net with SMTP id filter0187p3mdw1-23175-5D8D103D-65 2019-09-26 19:23:41.590499212 +0000 UTC m=+254396.222611587 Received: from herokuapp.com (unknown [34.207.207.12]) by ismtpd0010p1iad1.sendgrid.net (SG) with ESMTP id KlotMqngTzyS2RjaY8Ft_A for ; Thu, 26 Sep 2019 19:23:41.561 +0000 (UTC) Date: Thu, 26 Sep 2019 19:23:41 +0000 (UTC) From: zverok.offline@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 70672 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=2FmHQTWm54P5gda7opifcUPzJNIA2nrTK+T?= =?us-ascii?Q?0bXQXbMr9gCw4O6+uwcnaWHsYXthG2ewZ=2FWWam4?= =?us-ascii?Q?bki3Zex1EquvLYNg63UWiUZ7z99Fr5hDBhjPxmq?= =?us-ascii?Q?gWgCMoqMrWCbY6mdEB7YQfRbrbWS+1XlbHUuJkq?= =?us-ascii?Q?ZQzLcwH5OjD6LMbBc23is1awarZyc4qT49A=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 95117 Subject: [ruby-core:95117] [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 zverok (Victor Shepelev). > Duplicate of #11761 I don't think it is a duplicate, though I haven't clarified an important behavior indeed: ```ruby h = {a: 1, b: 2} h2 = h.with_default(3) h.default # => nil h2.default # => 3 ``` So, I propose side-effect-less method, that **is** acceptable for chaining, I believe. ---------------------------------------- Feature #16183: Hash#with_default https://bugs.ruby-lang.org/issues/16183#change-81751 * 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/