From: nobu@ruby-lang.org
To: ruby-core@ruby-lang.org
Subject: [ruby-core:100128] [Ruby master Feature#13683] Add strict Enumerable#single
Date: Fri, 25 Sep 2020 07:12:27 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-87697.20200925071227.1663@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-13683.20170627060807.1663@ruby-lang.org
Issue #13683 has been updated by nobu (Nobuyoshi Nakada).
Dan0042 (Daniel DeLorme) wrote in #note-25:
> > If collection is empty and a block was given, returns the block's return value:
>
> I really think the block form should be like find/select
>
> ```ruby
> [1,2,3].one{ _1.even? } #=> 2
> [1,2,3,4].one{ _1.even? } #=> error
> [1,2,3,4].one(nil){ _1.even? } #=> nil
> ```
It looks close to `Enumerable#one?` which counts truthy values only, but has a different semantics.
----------------------------------------
Feature #13683: Add strict Enumerable#single
https://bugs.ruby-lang.org/issues/13683#change-87697
* Author: dnagir (Dmytrii Nagirniak)
* Status: Feedback
* Priority: Normal
----------------------------------------
### 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/
next prev parent reply other threads:[~2020-09-25 7:12 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-13683.20170627060807.1663@ruby-lang.org>
2020-08-28 20:19 ` [ruby-core:99757] [Ruby master Feature#13683] Add strict Enumerable#single fatkodima123
2020-08-28 21:14 ` [ruby-core:99758] " marcandre-ruby-core
2020-08-29 4:23 ` [ruby-core:99765] " sawadatsuyoshi
2020-08-29 11:11 ` [ruby-core:99768] " daniel
2020-08-29 12:29 ` [ruby-core:99770] " sawadatsuyoshi
2020-08-29 12:42 ` [ruby-core:99772] " daniel
2020-08-30 11:26 ` [ruby-core:99778] " eregontp
2020-09-15 6:35 ` [ruby-core:100008] " shannonskipper
2020-09-18 20:25 ` [ruby-core:100044] " petrik
2020-09-25 7:12 ` nobu [this message]
2020-09-25 7:14 ` [ruby-core:100129] " mame
2020-10-02 14:14 ` [ruby-core:100280] " nobu
2020-10-06 21:06 ` [ruby-core:100316] " shannonskipper
2021-09-02 17:52 ` [ruby-core:105124] " meisel (Michael Eisel)
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-87697.20200925071227.1663@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).