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=AWL,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 558FA20209 for ; Wed, 28 Jun 2017 06:22:55 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 2068012080E; Wed, 28 Jun 2017 15:22:53 +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 EFDB4120793 for ; Wed, 28 Jun 2017 15:22:50 +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=FflwgTLhyys9GEma75g3pr+JPyE=; b=iTod5RIOLxHsAqz4WW W/4AcQ0gocFbFauKXV3+TwTwrXI3Ld2S4dhEAxOyhfz4oFXDebw3Cr/l0TKS5/k2 C9MdkNAnV41DFEHBbIt9nhSMN3KvBbVKilHmMIMVMxW0t6XRDDZ2dhz5tXMB6jnS P9U9jHjzSiM+fcp7urYozT5v4= Received: by filter0634p1mdw1.sendgrid.net with SMTP id filter0634p1mdw1-29399-59534B36-14 2017-06-28 06:22:46.2654141 +0000 UTC Received: from herokuapp.com (ec2-54-160-210-18.compute-1.amazonaws.com [54.160.210.18]) by ismtpd0002p1iad1.sendgrid.net (SG) with ESMTP id 83KdTmO-SBiTp8wTCr8neg Wed, 28 Jun 2017 06:22:46.235 +0000 (UTC) Date: Wed, 28 Jun 2017 06:22:46 +0000 From: nobu@ruby-lang.org To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 56829 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 13683 X-Redmine-Issue-Author: dnagir 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: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS6dMLI4vzMshLf6o6w/qgNTRb7t5Hlm1Li1lS m0c8tWL03mXC5peuD3C4+iqvfhPoJWwsJ4NBn9x5KaWthR6EUMaF0ZplVs7SjaTfl5lJAqN9yKm+w8 SJwcvJNZdMzjuTbh9z9/wOHu7QWIMRb6ds6VoRJg+u7hwlKmuF7ylhP+mQ== X-ML-Name: ruby-core X-Mail-Count: 81803 Subject: [ruby-core:81803] [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 nobu (Nobuyoshi Nakada). Description updated `Enumerable#first` returns not only the first element, the elements at the beginning up to the number given by an optional argument. How about an optional boolean argument `exact` to `Enumerable#first` or `Enumerable#take`? ---------------------------------------- Feature #13683: Add strict Enumerable#single https://bugs.ruby-lang.org/issues/13683#change-65503 * 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/