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.4 required=3.0 tests=BAYES_00,FROM_SUSPICIOUS_NTLD, HEADER_FROM_DIFFERENT_DOMAINS,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 D83F21F4BD for ; Sun, 6 Oct 2019 20:14:39 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 17BE412099F; Mon, 7 Oct 2019 05:14:26 +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 7B7A5120953 for ; Mon, 7 Oct 2019 05:14:24 +0900 (JST) Received: by filter0177p3mdw1.sendgrid.net with SMTP id filter0177p3mdw1-18688-5D9A4B20-3 2019-10-06 20:14:24.01027601 +0000 UTC m=+265680.698977605 Received: from herokuapp.com (unknown [34.201.109.215]) by ismtpd0036p1mdw1.sendgrid.net (SG) with ESMTP id 5mM1--PHR36cXcGFawTl_A for ; Sun, 06 Oct 2019 20:14:23.617 +0000 (UTC) Date: Sun, 06 Oct 2019 20:14:23 +0000 (UTC) From: jonathan@hefner.pro Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 70837 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 13683 X-Redmine-Issue-Author: dnagir X-Redmine-Sender: jonathanhefner 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?y87fQmSTkDB0MB8oF=2FSr+wCExKhWErwiIVXChRtBifvdlqkY2cSSiClPnbiXtf?= =?us-ascii?Q?0ZPMBTVwjNl9Aqiwd5WhNtl9O1j5OreSIa=2FYIpI?= =?us-ascii?Q?nAPNNncSZFVd79XxU6uItoiDTotD1HZVaU9pdY1?= =?us-ascii?Q?MbAKPRl3UEzy7ard6DOysVvOmPNGWLAE01S9kQ=2F?= =?us-ascii?Q?1=2FfvT1Ho9u5vKP2yQx=2FzFtmloKl8iKg=2Fmhw=3D=3D?= To: ruby-core@ruby-lang.org X-ML-Name: ruby-core X-Mail-Count: 95250 Subject: [ruby-core:95250] [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 jonathanhefner (Jonathan Hefner). matz (Yukihiro Matsumoto) wrote: > Hmm, I don't like the name `single`. Besides that, I think it may be useful for database access, but I don't see the use-case of this method for generic Enumerable. I use (monkey-patched) `Enumerable#single` in Ruby scripts which must fail fast when they encounter ambiguity. For example `Nokogiri::HTML(html).css(selector).single` to ensure an unambiguous matching HTML element. Or `Dir.glob(pattern).single` to ensure an unambiguous matching file. Also, I agree that `only` would be a better name. And it would read more naturally if accepting an `n` argument like `Enumerable#first` does. ---------------------------------------- Feature #13683: Add strict Enumerable#single https://bugs.ruby-lang.org/issues/13683#change-81927 * Author: dnagir (Dmytrii Nagirniak) * Status: Feedback * Priority: Normal * Assignee: * Target version: ---------------------------------------- ### 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/