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_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 10D5120248 for ; Mon, 18 Mar 2019 22:48:41 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 89B971215EE; Tue, 19 Mar 2019 07:48:36 +0900 (JST) Received: from o1678916x28.outbound-mail.sendgrid.net (o1678916x28.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 06E261214C7 for ; Tue, 19 Mar 2019 07:48:32 +0900 (JST) Received: by filter0068p3mdw1.sendgrid.net with SMTP id filter0068p3mdw1-917-5C90203F-19 2019-03-18 22:48:31.643173592 +0000 UTC m=+13850.545184151 Received: from herokuapp.com (unknown [3.90.163.81]) by ismtpd0061p1mdw1.sendgrid.net (SG) with ESMTP id vdzEz4ZBRDaclF4xQ1jU3w for ; Mon, 18 Mar 2019 22:48:31.527 +0000 (UTC) Date: Mon, 18 Mar 2019 22:48:32 +0000 (UTC) From: gray.kemmey@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 67330 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 4475 X-Redmine-Issue-Author: jordi X-Redmine-Issue-Assignee: nobu X-Redmine-Sender: gkemmey 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?LcAPWvJhOjDLxPgXEMVGA8GzLf4+AVUhALQpxIWK9D4xj9q+gI=2F5xkcb53TDMD?= =?us-ascii?Q?qSGFaU0+i56l8GiVLS7nrK4d9HBeWhSA0zSUFK5?= =?us-ascii?Q?u9bYEiNi+YuD1KAfLARVjYZWidWyvNh6IylO3g7?= =?us-ascii?Q?PSFR3Nfli0ICRoi4cixYIBtnLM=2Fx72uZtiRJj77?= =?us-ascii?Q?hW2fbw7yh7b9t8DXtVh6hF3f+8VHLZ4y45g=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 91878 Subject: [ruby-core:91878] [Ruby trunk Feature#4475] default variable name for parameter 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 #4475 has been updated by gkemmey (Gray Kemmey). Is there a reason to do this other than to save some typing? I'm not sure we've proposed any terribly real-world cases here where this made code better. I feel like we already have some single-character patterns around blocks with one or two arguments, that aren't that onerous to type and still convey meaning. For example: ```ruby 10.times { |i| ... } # i for index 10.times { |n| ... } # n for number [1, 2, 3].map { |n| ... } # n for number [1, 2, 3].map { |e| ... } # e for element ['one', 'two'].map { |s| ... } # s for string [1, 2, 3].each_with_index { |e, i| ... } # e for element, i for index { }.each { |k, v| ... } # k for key, v for value [[], []]each { |r, c| ... } # two-d arrays: r for row, c for column File.open('') { |f| ... } # f for file ``` For anything more complex, is it bad too *have* to name your arguments? In no particular order, some downsides I see: 1. It adds some cognitive load to remember the position of arguments. Like in the `{}.each` example -- is `@1` the key or the value? It's obvious when using `k` and `v` which is which. 2. It blurs the line between instance variables. 3. I don't know that we use simple blocks like this *most* of the time, which means, as a newcomer, you won't see this regularly, and it'll be confusing when you do. Think about the first time you saw something like `[1,2,3].reduce(&:+)`. Was that clear to anyone? (*Though in the `&` case, certainly worth the complexity.*) 4. It's always a little strange when things like this aren't 0-based, right? 5. Personally, find it a little jarring to iterate over an array and then use `@1` to reference each element. "At one" just *feels* positional (like an index). Anyway, this seems like it adds a fair amount of complexity and sacrifices a lot of readability, just to save ~2 characters. Happy to hear why I'm wrong though... ---------------------------------------- Feature #4475: default variable name for parameter https://bugs.ruby-lang.org/issues/4475#change-77146 * Author: jordi (jordi polo) * Status: Closed * Priority: Normal * Assignee: nobu (Nobuyoshi Nakada) * Target version: ---------------------------------------- =begin There is a very common pattern in Ruby: object.method do |variable_name| variable_name doing something end Many times in fact the name of the object is so self explanatory that we don't care about the name of the variable of the block. It is common to see things like : @my_sons.each { |s| s.sell_to_someone } or Account.all.each { |a| my_account << a.money } People tend to choose s or a because we have the class or the object name just there to remind you about the context. I would like to know if can be a good idea to have a default name for that parameter. I think it is Groovy that does something like: Account.all.each { my_account << it.money } Where it is automagically filled and it doesn't need to be declared. I think it is as readable or more (for newbies who don't know what is ||) and we save some typing :) =end -- https://bugs.ruby-lang.org/