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=AWL,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY 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 5CA091F8C6 for ; Thu, 2 Sep 2021 17:52:42 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 4F94E12098A; Fri, 3 Sep 2021 02:51:20 +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 C0FC312097F for ; Fri, 3 Sep 2021 02:51:18 +0900 (JST) Received: by filterdrecv-7bf5c69d5-88tll with SMTP id filterdrecv-7bf5c69d5-88tll-1-61310F65-32 2021-09-02 17:52:37.491096807 +0000 UTC m=+70336.341049510 Received: from herokuapp.com (unknown) by ismtpd0189p1mdw1.sendgrid.net (SG) with ESMTP id XQn42YKpT3iKLDlsPJwehQ for ; Thu, 02 Sep 2021 17:52:37.361 +0000 (UTC) Date: Thu, 02 Sep 2021 17:52:37 +0000 (UTC) From: "meisel (Michael Eisel)" Message-ID: References: Mime-Version: 1.0 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 13683 X-Redmine-Issue-Author: dnagir X-Redmine-Sender: meisel 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-Redmine-MailingListIntegration-Message-Ids: 81302 X-SG-EID: =?us-ascii?Q?KUq31=2Fvsdc2j4w+pddNDwvDkMkCWOTJ8FFQ7XEL83UP4qxqljTfcHLCX8cWxOM?= =?us-ascii?Q?9Lj27kyjNzwMAkR=2FAbsfM4EWDAsX0NVBerM7rRk?= =?us-ascii?Q?RNeSkbm441LebidUXJHwj05sOXCgkxOuXiYCeSq?= =?us-ascii?Q?3by6EI=2FJTrpTIIwlwhpO7sQFQxNUZxmHT10FN5=2F?= =?us-ascii?Q?haeaQ2VDMzsmdYmFX823q+J5h9n++Ua+v4nF8RU?= =?us-ascii?Q?RHcvf6gx7l54Pe9rrvEF0tcCelGUQWzU2OCYlqm?= =?us-ascii?Q?ELsM=2FR0RelSJgyOsBcPOw=3D=3D?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 105124 Subject: [ruby-core:105124] [Ruby master Feature#13683] Add strict Enumerable#single 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 #13683 has been updated by meisel (Michael Eisel). +1 for this, I've needed it in many cases, generally where I have to select an element from a set and there's no stable/documented way of doing this. So, I find some criteria that seem to work, but would like to protect against my criteria being wrong or invalidated by future changes. In the past, I've needed it for sets like sibling directories and sibling XML nodes. As for the name, I think `#take_only` would be another reasonable option ---------------------------------------- Feature #13683: Add strict Enumerable#single https://bugs.ruby-lang.org/issues/13683#change-93537 * Author: dnagir (Dmytrii Nagirniak) * Status: Feedback * Priority: Normal ---------------------------------------- ### Summary This is inspired by other languages and frameworks, such as LINQ's [Single](https://msdn.microsoft.com/en-us/library/bb155325%28v=vs.110%29.aspx) (pardon MSDN reference), which has very big distinction between `first` and `single` element of a collection. - `first` normally returns the top element, and the developer assumes there could be many; - `single` returns one and only one element, and it is an error if there are none or more than one. We, in Ruby world, very often write `fetch_by('something').first` assuming there's only one element that can be returned there. But in majority of the cases, we really want a `single` element. The problems with using `first` in this case: - developer needs to explicitly double check the result isn't `nil` - in case of corrupted data (more than one item returned), it will never be noticed `Enumerable#single` addresses those problems in a very strong and specific way that may save the world by simply switching from `first` to `single`. ### Other information - we may come with a better internal implementation (than `self.map`) - better name could be used, maybe `only` is better, or a bang version? - re-consider the "block" implementation in favour of a separate method (`single!`, `single_or { 'default' }`) The original implementation is on the ActiveSupport https://github.com/rails/rails/pull/26206 But it was suggested to discuss the possibility of adding it to Ruby which would be amazing. -- https://bugs.ruby-lang.org/