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=-3.0 required=3.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,RP_MATCHES_RCVD,SPF_PASS,T_DKIM_INVALID shortcircuit=no autolearn=ham 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 E267320281 for ; Mon, 25 Sep 2017 09:05:23 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 8AB6C1209CB; Mon, 25 Sep 2017 18:05:22 +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 8623E12091C for ; Mon, 25 Sep 2017 18:05:18 +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=7hzjs4BbITKj/k/af6+eUk+sV8c=; b=Pvg4I/I90+O9K1k0Ds AzhlT6aS6Q8t0vBxENrIXI+kSz1eplwDKGyivLQXtfQ+St6ykSWAl2bsl0xPu5IK A2f0hQ1Ck2bnpx1xgL0hxgpWPbDysqdtzS0QjYeDnY7hYynhQ7DaZATfHEsYN+r0 qtyR4JOKvOXiD1ByD7F1G7HOY= Received: by filter0011p3mdw1.sendgrid.net with SMTP id filter0011p3mdw1-10824-59C8C6CB-31 2017-09-25 09:05:15.462305131 +0000 UTC Received: from herokuapp.com (ec2-54-160-132-132.compute-1.amazonaws.com [54.160.132.132]) by ismtpd0025p1iad2.sendgrid.net (SG) with ESMTP id D2OZFWcqTAmxSgNxMBboQg Mon, 25 Sep 2017 09:05:15.405 +0000 (UTC) Date: Mon, 25 Sep 2017 09:05:16 +0000 (UTC) From: matz@ruby-lang.org To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 58084 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 13683 X-Redmine-Issue-Author: dnagir X-Redmine-Sender: matz 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/Ymy4QrNMhiuLXJG8OTL2vJD1yS7M7CUZXyIw2PYotAnmpf07eCYYvfhjfYSKmZ cuoa85ToQcWZSw9S2Yx6ri9t12A4gzv9r8PJ1FdGRCvmV1v5YVyakylRcZSMPwtX/ENuojRBuL6rqD DE2uo94fzj/PTuJTLA0ZVurx1jEfBCB2GHHezT9YEb1oO5tvZQe2utcljg== X-ML-Name: ruby-core X-Mail-Count: 82983 Subject: [ruby-core:82983] [Ruby trunk Feature#13683][Feedback] 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 matz (Yukihiro Matsumoto). Status changed from Open to Feedback 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. Matz. ---------------------------------------- Feature #13683: Add strict Enumerable#single https://bugs.ruby-lang.org/issues/13683#change-66891 * 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/