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=-3.7 required=3.0 tests=AWL,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 837061F461 for ; Tue, 27 Aug 2019 00:18:36 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 7F9B01209EE; Tue, 27 Aug 2019 09:18:28 +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 91D321209EE for ; Tue, 27 Aug 2019 09:18:25 +0900 (JST) Received: by filter0032p3las1.sendgrid.net with SMTP id filter0032p3las1-29431-5D6476D1-1 2019-08-27 00:18:25.087837525 +0000 UTC m=+14004.981461958 Received: from herokuapp.com (unknown [54.242.199.255]) by ismtpd0038p1iad2.sendgrid.net (SG) with ESMTP id 3Cj2gEvyR9K8xZwIw5-EMw for ; Tue, 27 Aug 2019 00:18:24.960 +0000 (UTC) Date: Tue, 27 Aug 2019 00:18:25 +0000 (UTC) From: nobu@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 70146 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16120 X-Redmine-Issue-Author: Dan0042 X-Redmine-Sender: nobu 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?q8Dly+pU2+3ektTtZVXgZtbJPXwqo7p86jCsvYTW4Bwpz=2FenIONc=2FVlTYrt=2Ft8?= =?us-ascii?Q?V8P5TE+sBx3koA4kWY4P9ZJ0OAX2Pt7RBfidGZH?= =?us-ascii?Q?QID657iayZMguFSMsN4YhjpqWBG2vPkgd9kDxqY?= =?us-ascii?Q?PsGCitVgrxzJ=2Fs=2FokLW76lyv3br28JROt0vueJD?= =?us-ascii?Q?sfqXln1vSgiTI2VvpxGpC+2u4aSHVLW52bw=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 94593 Subject: [ruby-core:94593] [Ruby master Feature#16120] Implicit block argument if block starts with dot-method call 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 #16120 has been updated by nobu (Nobuyoshi Nakada). https://github.com/nobu/ruby/tree/feature/implicit-block-param ---------------------------------------- Feature #16120: Implicit block argument if block starts with dot-method call https://bugs.ruby-lang.org/issues/16120#change-81069 * Author: Dan0042 (Daniel DeLorme) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- How about considering this syntax for implicit block parameter: ``` [10, 20, 30].map{ .to_s(16) } #=> ["a", "14", "1e"] ``` Infinite thanks to @maedi for [the idea](https://bugs.ruby-lang.org/issues/15723#note-19) This proposal is related to #4475, #10829, #12115, #15302, #15483, #15723, #15897, #16113 (and probably many others) which I feel are all trying to solve the same "problem". So I strongly believe all these feature requests should to be considered together in order to make a decision. This "problem" can be more-or-less stated thus: * There is a very common pattern in ruby: `posts.map{ |post| post.author.name }` * In that line, the three 3 "post" in close proximity feel redundant and not DRY. * To reduce the verbosity, people tend to use a meaningless one-character variable in the block * But even so `posts.map{ |p| p.author.name }` still feels redundant. * This "problem" is felt by many in the ruby community, and is the reason people often prefer `posts.map(&:author)` * But that only works for one method with no arguments. * This results in many requests for a block shorthand than can do more. I realize that many people feel this is not a problem at all and keep saying "just use regular block syntax". But the repeated requests over the years, as well as the widespread usage of `(&:to_s)`, definitely indicate this is a wish/need for a lot of people. Rather than adding to #15723 or #15897, I chose to make this a separate proposal because, unlike `it` or `@` implicit variables, it allows to simplify **only** `{ |x| x.foo }`, not `{ |x| foo(x) }`. This is on purpose and, in my opinion, a desirable limitation. The advantages are (all in my opinion, of course) * Extremely readable: `posts.map{ .author.name }` * Possibly even more than with an explicit variable. * Of all proposals this handles the most important use-case with the most elegant syntax. * It's better to have a beautiful shorthand for 90% of cases than a non-beautiful shorthand for 100% of cases. * A shorthand notation is less needed for `{ |x| foo(x) }` since the two `x` variables are further apart and don't feel so redundant. * No ascii soup * No potential incompatibility like `_` or `it` or `item` * Very simple to implement; there's just an implicit `|var| var` at the beginning of the block. * In a way it's similar to chaining methods on multiple lines: posts.map{ |post| post .author.name } It may be interesting to consider that the various proposals are not *necessarily* mutually exclusive. You *could* have `[1,2,3].map{ .itself + @ + @1 }`. Theoretically. I feel like I've wanted something like this for most of the 16 years I've been coding ruby. Like... **this** is what I wanted that `(&:to_s)` could only deliver half-way. I predict that if this syntax is accepted, most people using `(&:to_s)` will switch to this. -- https://bugs.ruby-lang.org/