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 AB89520209 for ; Wed, 28 Jun 2017 01:15:29 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 8B3E71207BB; Wed, 28 Jun 2017 10:15:27 +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 CEDE7120795 for ; Wed, 28 Jun 2017 10:15:22 +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=DEjSy3VEnxnm2Ek/mXy7VBNRhLA=; b=szz2jf5HrfCBTR+s8Y GF9opN1nhcPBLhNOPL1kxsrqYy5wqs3fMbY0yegoo//IN469zhdB4sNCgxLAeHAr 2veKtsyGUj2Vr6By9DHrFSa32PY7qeypltAhTJyaV8QVxCz2N7yh2NUvcltco1DI Xgy0tVSeB73Iw6lihUS53IZ9U= Received: by filter0498p1mdw1.sendgrid.net with SMTP id filter0498p1mdw1-4816-59530318-D 2017-06-28 01:15:04.110033553 +0000 UTC Received: from herokuapp.com (ec2-54-226-155-143.compute-1.amazonaws.com [54.226.155.143]) by ismtpd0005p1iad1.sendgrid.net (SG) with ESMTP id 8jsswTcfSXGhe4_ZTTUv-Q Wed, 28 Jun 2017 01:15:04.121 +0000 (UTC) Date: Wed, 28 Jun 2017 01:15:04 +0000 From: shannonskipper@gmail.com To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 56824 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 13683 X-Redmine-Issue-Author: dnagir X-Redmine-Sender: shan 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/Ymy4QrNMhiuLXJG8OTL2vJD1yS5Ggfh/xp281Unbp1m6hk9XQu/kfLOQl4a1qB d1U42fH5FQ41dV9FKbZPxuvgLIW8dvWka+vs1+W6aEW8iyNnnLVZzuZgIA61lzpksjKQFcZHjcOxMz 8fgwGFsu1SZ4Ee9g9VayfsSYwDygwZaDbVVDT4VueWRzpzjQKGEM14vliw== X-ML-Name: ruby-core X-Mail-Count: 81798 Subject: [ruby-core:81798] [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 shan (Shannon Skipper). shevegen (Robert A. Heiler) wrote: > What would the results be for the following code? In ruby (I find > it easier to read ruby code rather than the description actually): > > [].single > [1].single > [1,2].single > [1,2,3].single > > {}.single > {cat: 'Tom'}.single > {cat: 'Tom', mouse: 'Jerry'}.single > > (And any other Enumerable objects I may have forgotten here.) I wrote a quick implementation before realizing there was a link to a Rails PR. Here are the results of your examples (and one added): ~~~ module Enumerable def single if one? first else if block_given? yield else raise "wrong collection size (actual #{size || count}, expected 1)" end end end end [].single #!> RuntimeError: wrong collection size (actual 0, expected 1) [1].single #=> 1 [1,2].single #!> RuntimeError: wrong collection size (actual 2, expected 1) [1,2,3].single #!> RuntimeError: wrong collection size (actual 3, expected 1) {}.single #!> RuntimeError: wrong collection size (actual 0, expected 1) {cat: 'Tom'}.single #=> [:cat, "Tom"] {cat: 'Tom', mouse: 'Jerry'}.single #!> RuntimeError: wrong collection size (actual 2, expected 1) [].single { 42 } #=> 42 ~~~ ---------------------------------------- Feature #13683: Add strict Enumerable#single https://bugs.ruby-lang.org/issues/13683#change-65495 * 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/