From: shannonskipper@... Date: 2017-06-28T01:15:04+00:00 Subject: [ruby-core:81798] [Ruby trunk Feature#13683] Add strict Enumerable#single 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/ Unsubscribe: