From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) 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, DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_MED,RP_MATCHES_RCVD,SPF_PASS, T_DKIM_INVALID shortcircuit=no autolearn=no autolearn_force=no version=3.4.0 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 9BF561F600 for ; Mon, 24 Jul 2017 00:26:18 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id ACCE6120751; Mon, 24 Jul 2017 09:26:14 +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 1AC79120739 for ; Mon, 24 Jul 2017 09:26:11 +0900 (JST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=sendgrid.me; h=from:to:references:subject:mime-version:content-type:content-transfer-encoding:list-id; s=smtpapi; bh=8e6TgX880eYpQueAMXSVVpqdHqg=; b=lHJBdMqmtxcmdnYSjY /PEa4afkuevKDpwaxqlTpa4lv+WA5iBkX/tvtmLQYJ/WqSZ+Wiv1MAQ0XlBZPU3S bPfapFQvtWE64UePbQg+e4Ksj2jf0pp7dCC1qSqwXeh9VpuV7+hP8iRHMjQlj9+Z m/ZaEsYnTccQigVW25dwzR69I= Received: by filter1113p1mdw1.sendgrid.net with SMTP id filter1113p1mdw1-5357-59753E9D-22 2017-07-24 00:26:05.422365907 +0000 UTC Received: from herokuapp.com (ec2-54-221-134-236.compute-1.amazonaws.com [54.221.134.236]) by ismtpd0024p1mdw1.sendgrid.net (SG) with ESMTP id aekohxtrR2iD3T4igiM8ow Mon, 24 Jul 2017 00:26:05.376 +0000 (UTC) Date: Mon, 24 Jul 2017 00:26:06 +0000 (UTC) From: johncbackus@gmail.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 57176 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 13683 X-Redmine-Issue-Author: dnagir X-Redmine-Sender: backus 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: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS6Mb7rDfvOCKidbRGnZmuZhc3BxyKlJFS6+L5 QyK6XE0jl+1zkDe4qckVSbHJDn9B1xOiYAl3CJwoxE0MW93rASBifRBadP2Ai6rJKeJEM7UdGJuOaQ HvPgjMD3wJ9C3oliu4Sa/LTYhFhMyUi/1zDxLBdfgV7XfwvzyOzfMbXE/g== X-ML-Name: ruby-core X-Mail-Count: 82140 Subject: [ruby-core:82140] [Ruby trunk 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 backus (John Backus). +1 to this proposal!! I have a `Util.one(...)` method in a half dozen or more projects. IMO `#one` is a nicer name than `#single`. [ROM](https://github.com/rom-rb/rom/blob/6016d323ca0a2aa38167e84a4eb2da0384e75b13/core/lib/rom/relation/loaded.rb#L49-L77) exposes an interface I like when reading results from the db: - `#one!` - raise an error unless the result's `#size` is *exactly* `1` - `#one` - raise an error if the result's `#size` is greater than `1`. Return the result of `#first` otherwise (so an empty result returns `nil`). I don't think the implementation should use the `#one?` predicate though. It would be confusing if `[nil, true, false].single` gave you `nil` instead of raising an error. ---------------------------------------- Feature #13683: Add strict Enumerable#single https://bugs.ruby-lang.org/issues/13683#change-65898 * Author: dnagir (Dmytrii Nagirniak) * Status: Open * 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/