ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:81779] [Ruby trunk Bug#13683] Add strict Enumerable#single
       [not found] <redmine.issue-13683.20170627060807@ruby-lang.org>
@ 2017-06-27  6:08 ` dnagir
  2017-06-27 10:18 ` [ruby-core:81788] [Ruby trunk Feature#13683] " eregontp
                   ` (19 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: dnagir @ 2017-06-27  6:08 UTC (permalink / raw)
  To: ruby-core

Issue #13683 has been reported by dnagir (Dmytrii Nagirniak).

----------------------------------------
Bug #13683: Add strict Enumerable#single
https://bugs.ruby-lang.org/issues/13683

* Author: dnagir (Dmytrii Nagirniak)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: 2.4.1
* Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
### 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/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [ruby-core:81788] [Ruby trunk Feature#13683] Add strict Enumerable#single
       [not found] <redmine.issue-13683.20170627060807@ruby-lang.org>
  2017-06-27  6:08 ` [ruby-core:81779] [Ruby trunk Bug#13683] Add strict Enumerable#single dnagir
@ 2017-06-27 10:18 ` eregontp
  2017-06-27 20:05 ` [ruby-core:81793] " shevegen
                   ` (18 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: eregontp @ 2017-06-27 10:18 UTC (permalink / raw)
  To: ruby-core

Issue #13683 has been updated by Eregon (Benoit Daloze).


+1, I have found this useful a few times as well.
Usually, I just define my own on Array, but it makes sense as well for Enumerable.

----------------------------------------
Feature #13683: Add strict Enumerable#single
https://bugs.ruby-lang.org/issues/13683#change-65484

* 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/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [ruby-core:81793] [Ruby trunk Feature#13683] Add strict Enumerable#single
       [not found] <redmine.issue-13683.20170627060807@ruby-lang.org>
  2017-06-27  6:08 ` [ruby-core:81779] [Ruby trunk Bug#13683] Add strict Enumerable#single dnagir
  2017-06-27 10:18 ` [ruby-core:81788] [Ruby trunk Feature#13683] " eregontp
@ 2017-06-27 20:05 ` shevegen
  2017-06-27 21:41   ` [ruby-core:81794] " Matthew Kerwin
  2017-06-27 23:19 ` [ruby-core:81796] " mame
                   ` (17 subsequent siblings)
  20 siblings, 1 reply; 22+ messages in thread
From: shevegen @ 2017-06-27 20:05 UTC (permalink / raw)
  To: ruby-core

Issue #13683 has been updated by shevegen (Robert A. Heiler).


I am not against or in favour of it but just a question.

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.)


----------------------------------------
Feature #13683: Add strict Enumerable#single
https://bugs.ruby-lang.org/issues/13683#change-65489

* 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/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [ruby-core:81794] Re: [Ruby trunk Feature#13683] Add strict Enumerable#single
  2017-06-27 20:05 ` [ruby-core:81793] " shevegen
@ 2017-06-27 21:41   ` Matthew Kerwin
  0 siblings, 0 replies; 22+ messages in thread
From: Matthew Kerwin @ 2017-06-27 21:41 UTC (permalink / raw)
  To: Ruby developers


[-- Attachment #1.1: Type: text/plain, Size: 776 bytes --]

On 28 Jun. 2017 06:06, <shevegen@gmail.com> wrote:

Issue #13683 has been updated by shevegen (Robert A. Heiler).


I am not against or in favour of it but just a question.

What would the results be for the following code? In ruby (I find
it easier to read ruby code rather than the description actually):


I'm not sure the description is that hard to read... Is the same as #first
but raises an exception of the enum doesn't have exactly one element.


    [].single
    [1].single
    [1,2].single
    [1,2,3].single


Err, 1, err, err


    {}.single
    {cat: 'Tom'}.single
    {cat: 'Tom', mouse: 'Jerry'}.single


Err, ['cat', 'Tom'], err


    (And any other Enumerable objects I may have forgotten here.)


Same as .to_a.single I suppose.

Cheers
-- 
Matthew Kerwin

[-- Attachment #1.2: Type: text/html, Size: 2166 bytes --]

[-- Attachment #2: Type: text/plain, Size: 138 bytes --]


Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [ruby-core:81796] [Ruby trunk Feature#13683] Add strict Enumerable#single
       [not found] <redmine.issue-13683.20170627060807@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2017-06-27 20:05 ` [ruby-core:81793] " shevegen
@ 2017-06-27 23:19 ` mame
  2017-06-28  1:15 ` [ruby-core:81798] " shannonskipper
                   ` (16 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: mame @ 2017-06-27 23:19 UTC (permalink / raw)
  To: ruby-core

Issue #13683 has been updated by mame (Yusuke Endoh).


+1.  I always feel uncomfortable whenever using `first` for this purpose.

----------------------------------------
Feature #13683: Add strict Enumerable#single
https://bugs.ruby-lang.org/issues/13683#change-65491

* 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/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [ruby-core:81798] [Ruby trunk Feature#13683] Add strict Enumerable#single
       [not found] <redmine.issue-13683.20170627060807@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2017-06-27 23:19 ` [ruby-core:81796] " mame
@ 2017-06-28  1:15 ` shannonskipper
  2017-06-28  6:22 ` [ruby-core:81803] " nobu
                   ` (15 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: shannonskipper @ 2017-06-28  1:15 UTC (permalink / raw)
  To: 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/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [ruby-core:81803] [Ruby trunk Feature#13683] Add strict Enumerable#single
       [not found] <redmine.issue-13683.20170627060807@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2017-06-28  1:15 ` [ruby-core:81798] " shannonskipper
@ 2017-06-28  6:22 ` nobu
  2017-06-30 13:57 ` [ruby-core:81874] " dnagir
                   ` (14 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: nobu @ 2017-06-28  6:22 UTC (permalink / raw)
  To: 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/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [ruby-core:81874] [Ruby trunk Feature#13683] Add strict Enumerable#single
       [not found] <redmine.issue-13683.20170627060807@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2017-06-28  6:22 ` [ruby-core:81803] " nobu
@ 2017-06-30 13:57 ` dnagir
  2017-06-30 14:01 ` [ruby-core:81875] " dnagir
                   ` (13 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: dnagir @ 2017-06-30 13:57 UTC (permalink / raw)
  To: ruby-core

Issue #13683 has been updated by dnagir (Dmytrii Nagirniak).


shevegen (Robert A. Heiler) wrote:
> What would the results be for the following code?

I would expect the following:

```ruby
[].single # => error
[1].single # =>1
[1,2].single # => error
[1,2,3].single # => error
 
{}.single # => error
{cat: 'Tom'}.single # same as .first => [:cat, 'Tom']
{cat: 'Tom', mouse: 'Jerry'}.single # error
```

----------------------------------------
Feature #13683: Add strict Enumerable#single
https://bugs.ruby-lang.org/issues/13683#change-65603

* 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/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [ruby-core:81875] [Ruby trunk Feature#13683] Add strict Enumerable#single
       [not found] <redmine.issue-13683.20170627060807@ruby-lang.org>
                   ` (6 preceding siblings ...)
  2017-06-30 13:57 ` [ruby-core:81874] " dnagir
@ 2017-06-30 14:01 ` dnagir
  2017-07-24  0:26 ` [ruby-core:82140] " johncbackus
                   ` (12 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: dnagir @ 2017-06-30 14:01 UTC (permalink / raw)
  To: ruby-core

Issue #13683 has been updated by dnagir (Dmytrii Nagirniak).


nobu (Nobuyoshi Nakada) wrote:
> `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`?

The purpose of the `single` suggested is to return one and only one element.
So it doesn't seem right to mix it up with `first` as it'll only add confusion, especially when used with a block.

On the other hand, I feel like a separate method that does one small thing well would be a much better API.


----------------------------------------
Feature #13683: Add strict Enumerable#single
https://bugs.ruby-lang.org/issues/13683#change-65604

* 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/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [ruby-core:82140] [Ruby trunk Feature#13683] Add strict Enumerable#single
       [not found] <redmine.issue-13683.20170627060807@ruby-lang.org>
                   ` (7 preceding siblings ...)
  2017-06-30 14:01 ` [ruby-core:81875] " dnagir
@ 2017-07-24  0:26 ` johncbackus
  2017-09-25  9:05 ` [ruby-core:82983] [Ruby trunk Feature#13683][Feedback] " matz
                   ` (11 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: johncbackus @ 2017-07-24  0:26 UTC (permalink / raw)
  To: 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/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [ruby-core:82983] [Ruby trunk Feature#13683][Feedback] Add strict Enumerable#single
       [not found] <redmine.issue-13683.20170627060807@ruby-lang.org>
                   ` (8 preceding siblings ...)
  2017-07-24  0:26 ` [ruby-core:82140] " johncbackus
@ 2017-09-25  9:05 ` matz
  2018-04-16 22:45 ` [ruby-core:86554] [Ruby trunk Feature#13683] " me
                   ` (10 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: matz @ 2017-09-25  9:05 UTC (permalink / raw)
  To: 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/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [ruby-core:86554] [Ruby trunk Feature#13683] Add strict Enumerable#single
       [not found] <redmine.issue-13683.20170627060807@ruby-lang.org>
                   ` (9 preceding siblings ...)
  2017-09-25  9:05 ` [ruby-core:82983] [Ruby trunk Feature#13683][Feedback] " matz
@ 2018-04-16 22:45 ` me
  2018-04-24 12:40 ` [ruby-core:86665] " nobu
                   ` (9 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: me @ 2018-04-16 22:45 UTC (permalink / raw)
  To: ruby-core

Issue #13683 has been updated by IotaSpencer (Ken Spencer).


matz (Yukihiro Matsumoto) wrote:
> 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.

I think of single as a method towards mutual exclusivity.
If an Array or Enumerable from another expression should only have a single element,
then this gives the process a much faster setup and possible rescue, as I currently have
one of my projects checking for the existence of 3 headers, `X-GitHub-Event`, `X-GitLab-Event`,
and `X-Gogs-Event`, and I found the easiest way was to use `one` from Enumerable, but I wanted it
to error out so that I could catch it with the rest of my raised exceptions from other errors that 
arise in the handling of the request.

How about these for suggestions.

`one_or_raise`
`one_or_nothing`

Part of my code for context.

~~~ ruby
      events = {'github' => github, 'gitlab' => gitlab, 'gogs' => gogs
      }
      events_m_e = events.values.one?
      case events_m_e
        when true
          event = 'push'
          service = events.select { |key, value| value }.keys.first
        when false
          halt 400, {'Content-Type' => 'application/json'}, {message: 'events are mutually exclusive', status: 'failure'
          }.to_json

        else halt 400, {'Content-Type' => 'application/json'}, {'status': 'failure', 'message': 'something weird happened'
        }
      end
~~~


----------------------------------------
Feature #13683: Add strict Enumerable#single
https://bugs.ruby-lang.org/issues/13683#change-71494

* 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/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [ruby-core:86665] [Ruby trunk Feature#13683] Add strict Enumerable#single
       [not found] <redmine.issue-13683.20170627060807@ruby-lang.org>
                   ` (10 preceding siblings ...)
  2018-04-16 22:45 ` [ruby-core:86554] [Ruby trunk Feature#13683] " me
@ 2018-04-24 12:40 ` nobu
  2018-07-26 20:40 ` [ruby-core:88129] " shannonskipper
                   ` (8 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: nobu @ 2018-04-24 12:40 UTC (permalink / raw)
  To: ruby-core

Issue #13683 has been updated by nobu (Nobuyoshi Nakada).


How about `Enumerable#just(num=1)` or `Enumerable#only(num=1)`?


----------------------------------------
Feature #13683: Add strict Enumerable#single
https://bugs.ruby-lang.org/issues/13683#change-71625

* 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/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [ruby-core:88129] [Ruby trunk Feature#13683] Add strict Enumerable#single
       [not found] <redmine.issue-13683.20170627060807@ruby-lang.org>
                   ` (11 preceding siblings ...)
  2018-04-24 12:40 ` [ruby-core:86665] " nobu
@ 2018-07-26 20:40 ` shannonskipper
  2019-04-02 16:11 ` [ruby-core:92111] " lisa.ugray
                   ` (7 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: shannonskipper @ 2018-07-26 20:40 UTC (permalink / raw)
  To: ruby-core

Issue #13683 has been updated by shan (Shannon Skipper).


nobu (Nobuyoshi Nakada) wrote:
> How about `Enumerable#just(num=1)` or `Enumerable#only(num=1)`?

Or maybe a slightly more verbose `Enumerable#first_and_only(num = 1)`?

----------------------------------------
Feature #13683: Add strict Enumerable#single
https://bugs.ruby-lang.org/issues/13683#change-73152

* 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/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [ruby-core:92111] [Ruby trunk Feature#13683] Add strict Enumerable#single
       [not found] <redmine.issue-13683.20170627060807@ruby-lang.org>
                   ` (12 preceding siblings ...)
  2018-07-26 20:40 ` [ruby-core:88129] " shannonskipper
@ 2019-04-02 16:11 ` lisa.ugray
  2019-10-06 20:14 ` [ruby-core:95250] [Ruby master " jonathan
                   ` (6 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: lisa.ugray @ 2019-04-02 16:11 UTC (permalink / raw)
  To: ruby-core

Issue #13683 has been updated by lugray (Lisa Ugray).


I was pointed here after sharing the following code with my team mates.  I really like the idea, and find I often reach for it.  What about the name `only`?
``` ruby
module Enumerable
  def only
    only!
  rescue IndexError
    nil
  end

  def only!
    raise(IndexError, "Count (#{count}) is not 1") if count != 1
    first
  end
end
```


----------------------------------------
Feature #13683: Add strict Enumerable#single
https://bugs.ruby-lang.org/issues/13683#change-77438

* 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/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [ruby-core:95250] [Ruby master Feature#13683] Add strict Enumerable#single
       [not found] <redmine.issue-13683.20170627060807@ruby-lang.org>
                   ` (13 preceding siblings ...)
  2019-04-02 16:11 ` [ruby-core:92111] " lisa.ugray
@ 2019-10-06 20:14 ` jonathan
  2019-10-07  2:23 ` [ruby-core:95254] " daniel
                   ` (5 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: jonathan @ 2019-10-06 20:14 UTC (permalink / raw)
  To: ruby-core

Issue #13683 has been updated by jonathanhefner (Jonathan Hefner).


matz (Yukihiro Matsumoto) wrote:
> 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.

I use (monkey-patched) `Enumerable#single` in Ruby scripts which must fail fast when they encounter ambiguity.  For example `Nokogiri::HTML(html).css(selector).single` to ensure an unambiguous matching HTML element.  Or `Dir.glob(pattern).single` to ensure an unambiguous matching file.

Also, I agree that `only` would be a better name.  And it would read more naturally if accepting an `n` argument like `Enumerable#first` does.


----------------------------------------
Feature #13683: Add strict Enumerable#single
https://bugs.ruby-lang.org/issues/13683#change-81927

* 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/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [ruby-core:95254] [Ruby master Feature#13683] Add strict Enumerable#single
       [not found] <redmine.issue-13683.20170627060807@ruby-lang.org>
                   ` (14 preceding siblings ...)
  2019-10-06 20:14 ` [ruby-core:95250] [Ruby master " jonathan
@ 2019-10-07  2:23 ` daniel
  2019-10-17  5:59 ` [ruby-core:95382] " matz
                   ` (4 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: daniel @ 2019-10-07  2:23 UTC (permalink / raw)
  To: ruby-core

Issue #13683 has been updated by Dan0042 (Daniel DeLorme).


+1

I actually have this as `single` in my own code, but `only` sounds fine also. I'd want a non-raising version (perhaps via a `raise` keyword arg?), as my usage tends to be like this:

```ruby
if match = filenames.select{ |f| f.start_with?(prefix) }.single
  redirect_to match
end
```

----------------------------------------
Feature #13683: Add strict Enumerable#single
https://bugs.ruby-lang.org/issues/13683#change-81931

* 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/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [ruby-core:95382] [Ruby master Feature#13683] Add strict Enumerable#single
       [not found] <redmine.issue-13683.20170627060807@ruby-lang.org>
                   ` (15 preceding siblings ...)
  2019-10-07  2:23 ` [ruby-core:95254] " daniel
@ 2019-10-17  5:59 ` matz
  2019-10-17  6:24 ` [ruby-core:95384] " ppyd
                   ` (3 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: matz @ 2019-10-17  5:59 UTC (permalink / raw)
  To: ruby-core

Issue #13683 has been updated by matz (Yukihiro Matsumoto).


I don't like `only` either since these names do not describe the behavior.

Matz.

----------------------------------------
Feature #13683: Add strict Enumerable#single
https://bugs.ruby-lang.org/issues/13683#change-82093

* 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/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [ruby-core:95384] [Ruby master Feature#13683] Add strict Enumerable#single
       [not found] <redmine.issue-13683.20170627060807@ruby-lang.org>
                   ` (16 preceding siblings ...)
  2019-10-17  5:59 ` [ruby-core:95382] " matz
@ 2019-10-17  6:24 ` ppyd
  2019-10-17  7:24 ` [ruby-core:95388] " hanmac
                   ` (2 subsequent siblings)
  20 siblings, 0 replies; 22+ messages in thread
From: ppyd @ 2019-10-17  6:24 UTC (permalink / raw)
  To: ruby-core

Issue #13683 has been updated by kinoppyd (Yasuhiro Kinoshita).


```
[1, 2].mono
[1, 2].solo
[1, 2].alone
```

----------------------------------------
Feature #13683: Add strict Enumerable#single
https://bugs.ruby-lang.org/issues/13683#change-82095

* 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/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [ruby-core:95388] [Ruby master Feature#13683] Add strict Enumerable#single
       [not found] <redmine.issue-13683.20170627060807@ruby-lang.org>
                   ` (17 preceding siblings ...)
  2019-10-17  6:24 ` [ruby-core:95384] " ppyd
@ 2019-10-17  7:24 ` hanmac
  2019-10-17 17:55 ` [ruby-core:95399] " daniel
  2019-11-13 16:04 ` [ruby-core:95845] " kuchenbecker.k
  20 siblings, 0 replies; 22+ messages in thread
From: hanmac @ 2019-10-17  7:24 UTC (permalink / raw)
  To: ruby-core

Issue #13683 has been updated by Hanmac (Hans Mackowiak).


Dan0042 (Daniel DeLorme) wrote:
> +1
> 
> I actually have this as `single` in my own code, but `only` sounds fine also. I'd want a non-raising version (perhaps via a `raise` keyword arg?), as my usage tends to be like this:
> 
> ```ruby
> if match = filenames.select{ |f| f.start_with?(prefix) }.single
>   redirect_to match
> end
> ```

instead of `#select`, shouldn't you use `#find` so it doesn't need to check the others when it already found a match?

----------------------------------------
Feature #13683: Add strict Enumerable#single
https://bugs.ruby-lang.org/issues/13683#change-82101

* 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/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [ruby-core:95399] [Ruby master Feature#13683] Add strict Enumerable#single
       [not found] <redmine.issue-13683.20170627060807@ruby-lang.org>
                   ` (18 preceding siblings ...)
  2019-10-17  7:24 ` [ruby-core:95388] " hanmac
@ 2019-10-17 17:55 ` daniel
  2019-11-13 16:04 ` [ruby-core:95845] " kuchenbecker.k
  20 siblings, 0 replies; 22+ messages in thread
From: daniel @ 2019-10-17 17:55 UTC (permalink / raw)
  To: ruby-core

Issue #13683 has been updated by Dan0042 (Daniel DeLorme).


> instead of `#select`, shouldn't you use `#find` so it doesn't need to check the others when it already found a match?

No, because it should return nil when there's more than one match.

----------------------------------------
Feature #13683: Add strict Enumerable#single
https://bugs.ruby-lang.org/issues/13683#change-82122

* 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/

^ permalink raw reply	[flat|nested] 22+ messages in thread

* [ruby-core:95845] [Ruby master Feature#13683] Add strict Enumerable#single
       [not found] <redmine.issue-13683.20170627060807@ruby-lang.org>
                   ` (19 preceding siblings ...)
  2019-10-17 17:55 ` [ruby-core:95399] " daniel
@ 2019-11-13 16:04 ` kuchenbecker.k
  20 siblings, 0 replies; 22+ messages in thread
From: kuchenbecker.k @ 2019-11-13 16:04 UTC (permalink / raw)
  To: ruby-core

Issue #13683 has been updated by kaikuchn (Kai Kuchenbecker).


I like `one` a lot. Especially since there's already `one?`.

----------------------------------------
Feature #13683: Add strict Enumerable#single
https://bugs.ruby-lang.org/issues/13683#change-82678

* 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/

^ permalink raw reply	[flat|nested] 22+ messages in thread

end of thread, other threads:[~2019-11-13 16:04 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-13683.20170627060807@ruby-lang.org>
2017-06-27  6:08 ` [ruby-core:81779] [Ruby trunk Bug#13683] Add strict Enumerable#single dnagir
2017-06-27 10:18 ` [ruby-core:81788] [Ruby trunk Feature#13683] " eregontp
2017-06-27 20:05 ` [ruby-core:81793] " shevegen
2017-06-27 21:41   ` [ruby-core:81794] " Matthew Kerwin
2017-06-27 23:19 ` [ruby-core:81796] " mame
2017-06-28  1:15 ` [ruby-core:81798] " shannonskipper
2017-06-28  6:22 ` [ruby-core:81803] " nobu
2017-06-30 13:57 ` [ruby-core:81874] " dnagir
2017-06-30 14:01 ` [ruby-core:81875] " dnagir
2017-07-24  0:26 ` [ruby-core:82140] " johncbackus
2017-09-25  9:05 ` [ruby-core:82983] [Ruby trunk Feature#13683][Feedback] " matz
2018-04-16 22:45 ` [ruby-core:86554] [Ruby trunk Feature#13683] " me
2018-04-24 12:40 ` [ruby-core:86665] " nobu
2018-07-26 20:40 ` [ruby-core:88129] " shannonskipper
2019-04-02 16:11 ` [ruby-core:92111] " lisa.ugray
2019-10-06 20:14 ` [ruby-core:95250] [Ruby master " jonathan
2019-10-07  2:23 ` [ruby-core:95254] " daniel
2019-10-17  5:59 ` [ruby-core:95382] " matz
2019-10-17  6:24 ` [ruby-core:95384] " ppyd
2019-10-17  7:24 ` [ruby-core:95388] " hanmac
2019-10-17 17:55 ` [ruby-core:95399] " daniel
2019-11-13 16:04 ` [ruby-core:95845] " kuchenbecker.k

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).