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 927081F4C0 for ; Thu, 31 Oct 2019 13:47:17 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 1E369120A35; Thu, 31 Oct 2019 22:47:07 +0900 (JST) Received: from xtrwkhkc.outbound-mail.sendgrid.net (xtrwkhkc.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id 99AC0120A04 for ; Thu, 31 Oct 2019 22:47:04 +0900 (JST) Received: by filter0159p3mdw1.sendgrid.net with SMTP id filter0159p3mdw1-2078-5DBAE5D9-CD 2019-10-31 13:47:05.552986245 +0000 UTC m=+224312.251648064 Received: from herokuapp.com (unknown [54.210.198.90]) by ismtpd0069p1iad1.sendgrid.net (SG) with ESMTP id 3YorauhhRq2XfdO0blLolw for ; Thu, 31 Oct 2019 13:47:05.500 +0000 (UTC) Date: Thu, 31 Oct 2019 13:47:05 +0000 (UTC) From: nobu@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 71201 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 16287 X-Redmine-Issue-Author: shevegen 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+3ektTtZVXgZtbJPXwqo7p86jCsvYTW4Bwhi69RLsonPt7xx+YcVP?= =?us-ascii?Q?zcCVIHjG241IdePDIY2QSSqGCAEOInQSmWcYbfA?= =?us-ascii?Q?MvC8TiX+gx2CUzfe8Ad7z5yxH2Rs78f5bIE7VJL?= =?us-ascii?Q?+XmvIyz+mHB6erLvov3902bCEDfmcBc46Afolbh?= =?us-ascii?Q?++IUYkkaq8y8=2FjfcwA7Ufr32+jY8Clhvg9A=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 95608 Subject: [ruby-core:95608] [Ruby master Feature#16287] Proposal to add .second and .third in particular to class Array 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 #16287 has been updated by nobu (Nobuyoshi Nakada). Description updated If I need such repeating accesses, I'll use `values_at` instead. Probably, the word `first` is misleading. If it were `head` or `lead`, the meaning would be clear. ---------------------------------------- Feature #16287: Proposal to add .second and .third in particular to class Array https://bugs.ruby-lang.org/issues/16287#change-82397 * Author: shevegen (Robert A. Heiler) * Status: Open * Priority: Normal * Assignee: * Target version: ---------------------------------------- I meant to suggest this earlier, but then I think I did not; if I actually did, then please excuse my forgetfulness and close the issue request here. I am very unorganized (also in reallife). Anyway - I will be as short as possible here. I would like to propose that we add `.second` and `.third`, in particular for `Array`s. I don't care that much for other data structures, so I will limit my initial suggestion here to the "minimum" case. I will next explain as to why I suggest it (as many ruby core members said before, to demonstrate the use case, needs, trade-offs etc...). Consider the following `Array`, aptly called array: ```ruby array = ['cat','dog','horse','fox','snake'] ``` So five elements, five animals. The archetypical "default" (main) way to query the "slot" at a specific position, in ruby, is via the index number, and []. So for example: ```ruby array[2] # => "horse" ``` So in this case we asked the array object for its third element (array index starts at 0). This is all fine; we ruby users use this a lot. We also have this available in ruby: ```ruby array.first # => "cat" array.last # => "snake" ``` This is often useful too, and I personally like this because it reads "naturally". But I think most of us may agree that the index specifier via [] is the main way for ruby to query elements at a certain position. We can also custom-define [] on our classes to allow for the [] to work (as syntactic sugar) on our own custom classes. Now consider the following situation - we wish to return the first element, the last, and the second element. This way works: ```ruby array[0] array[1] array[-1] ``` This way also works: ```ruby array.first array[1] array.last ``` The last solution, however had, puts me a bit off, in the sense that I dislike the mixing of `[]` with longer `.method_name`, such as `.first` and `.last`. In the last case, I think this would read better: ```ruby array.first array.second array.last ``` I do not know if this has been suggested before; quite possibly it may have been suggested before. If anyone knows then perhaps there was a reason why the latter was not added. To me, personally, from my point of view, I like a "symmetry" in the code here. I have found in my own code when I end up with a situation like this: ```ruby array.first array[1] array.last ``` Then I tend to much prefer that one instead: ```ruby array[0] array[1] array[-1] # Or another way to get the last element such as array.size instead. ``` The reason for the latter is mostly that I feel it "reads" better. But if I use `.first` and `.last`, then I would prefer a positional name, such as `.second`. You may wonder why I do not suggest `.fourth`, `.fifth` and so forth. Well, yes, this could be done too, but I think people will use these variants less and less. How often may it happen that you wanted to use `.fifth`, aka `[4]`? Probably not that often compared to `.first`, or `.last`, or even `[1]`. So my reasoning here is mostly about common use cases; I think `.second` and perhaps `.third` will be more common than e. g. .fifth. `.second` is probably significantly more frequent than `.third` too - but I have no problem if more names would be available either. To me this is mostly a detail, not the big picture. I am trying to keep my suggestion here smallish, though, to make it simpler if it is decided that this may be a useful or wanted addition. I should also add that I do not have any problem keeping using the `[]` variants. My use case is really motivated almost exclusively from `.first` and `.last` alone. These two names are probably by far the most commonly used method names, but I think `.second` may also be useful. And my reasoning is really mostly with the above explanation alone e. g. "symmetry" in code use between `array.first` and `array[1]`. Please feel free to comment in any way as you see fit. I think this was perhaps suggested before but I can not find any older discussion (or perhaps it was many years ago or so, in which case it may be useful to have this more recent discussion too, if only to be able to point others to it if it may come up again in the future). I also agree that the real benefit will be very small; at the same time, there should not be any major problem with the feature either. (Although, perhaps if matz may approve, it should come after ruby 3.0, just to not confuse people about this working; it may be easier to add it past ruby 3.0, so that ruby users do not have to wonder for now whether more method names to numbers may be available. I assume that Active* in rails may have this, but I don't know, I am still not using rails really, so I come from a non-rails point of view as well; I do use sinatra though, it's great - minimal code is great for being able to "build" flexible software.) PS: I limited this to class `Array`. Perhaps the above would fit to other enumerable or enumerators (I mix these up all the time), but my common use case is really motivated almost exclusively by and for class `Array`. The reason is because I tend to end up with array data structures often; they are so easy to work with in ruby. -- https://bugs.ruby-lang.org/