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=-4.1 required=3.0 tests=BAYES_00,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 C5CFD1F464 for ; Thu, 28 Nov 2019 06:25:11 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 9FA34120B4B; Thu, 28 Nov 2019 15:25:01 +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 1239B120B3C for ; Thu, 28 Nov 2019 15:24:59 +0900 (JST) Received: by filter0081p3las1.sendgrid.net with SMTP id filter0081p3las1-16527-5DDF683B-11 2019-11-28 06:24:59.743299372 +0000 UTC m=+714790.191902628 Received: from herokuapp.com (unknown [3.87.7.191]) by ismtpd0100p1iad2.sendgrid.net (SG) with ESMTP id Ofr1RVlKREmqTCcMazeKxw for ; Thu, 28 Nov 2019 06:24:59.522 +0000 (UTC) Date: Thu, 28 Nov 2019 06:24:59 +0000 (UTC) From: knu@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 71646 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16261 X-Redmine-Issue-Author: zverok X-Redmine-Sender: knu 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?hDsQvKErLSJ7pAc+tDc0a0gaAYeRBj5seyyxgbnjKEni3BNNKpparWwpXM2zf+?= =?us-ascii?Q?qNqjAAljlKGR5ZbQiW4gXe5mlyKU=2FmK4V2z7Hzl?= =?us-ascii?Q?Z3je96C4CK5Vhxo6mR+Hp6R0m4Da4EAZZqPdY8t?= =?us-ascii?Q?P6YOOarpFPr4fhHY0coRDi+w9VIw9NXOdjHJYak?= =?us-ascii?Q?eAcJ2RqAlOxNDcdkiKUeFV2PTOsaB=2FamoQw=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 96010 Subject: [ruby-core:96010] [Ruby master Feature#16261] Enumerable#each_splat and Enumerator#splat 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 #16261 has been updated by knu (Akinori MUSHA). I agree this feature would be a nice addition. Actually I had exactly the same idea, presented at Rails Developer Meetup 2019: https://www.slideshare.net/akinorimushaevolution-of-enumerator There's a subtle difference between Hash#each/map and Hash#select/reject in how they yield each key-value pair. ```ruby {a:1,b:2}.select{|x|p x} # :a # :b {a:1,b:2}.each{|x|p x} # [:a, 1] # [:b, 2] ``` I guess this was an unintended difference, but we cannot fix it by now for compatibility reasons, and each_splat would be one way to work around it. ---------------------------------------- Feature #16261: Enumerable#each_splat and Enumerator#splat https://bugs.ruby-lang.org/issues/16261#change-82848 * Author: zverok (Victor Shepelev) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- **UPD:** After discussion in comments, method names changed to "splat"-based. New methods proposal. Prototype code: ```ruby module Enumerable def each_splat return to_enum(__method__) unless block_given? each_entry { |item| yield(*item) } # unpacking possible array into several args end end class Enumerator def splat return to_enum(:splat) unless block_given? each_entry { |item| yield(*item) } end end ``` Supposed documentation/explanation: > For enumerable with Array items, passes all items in the block provided as a separate arguments. t could be useful if the provided block has lambda semantics, e.g. doesn't unpack arguments automatically. For example: ```ruby files = ["README.md", "LICENSE.txt", "Contributing.md"] content = [fetch_readme, fetch_license, fetch_contributing] # somehow make a content for the files files.zip(content).each_splat(&File.:write) # writes to each file its content ``` > When no block passed, returns enumerator of the tuples: ```ruby [1, 2, 3].zip([4, 5, 6]).each_splat.map(&:+) # => [5, 7, 9] ``` -- https://bugs.ruby-lang.org/