ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: nobu@ruby-lang.org
To: ruby-core@ruby-lang.org
Subject: [ruby-core:86665] [Ruby trunk Feature#13683] Add strict Enumerable#single
Date: Tue, 24 Apr 2018 12:40:30 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-71625.20180424124029.8ffd296a05b52786@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-13683.20170627060807@ruby-lang.org

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/

  parent reply	other threads:[~2018-04-24 12:40 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [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 ` nobu [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-71625.20180424124029.8ffd296a05b52786@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).