[#99426] [Ruby master Bug#17098] Float#negative? reports negative zero as not negative — chris@...

Issue #17098 has been reported by chrisseaton (Chris Seaton).

12 messages 2020/08/01

[#99449] [Ruby master Bug#17100] Ractor: a proposal for new concurrent abstraction without thread-safety issues — ko1@...

Issue #17100 has been reported by ko1 (Koichi Sasada).

41 messages 2020/08/03

[#99474] [Ruby master Feature#17103] Add a :since option to ObjectSpace.dump_all — jean.boussier@...

Issue #17103 has been reported by byroot (Jean Boussier).

9 messages 2020/08/04

[#99485] [Ruby master Misc#17104] Why are interpolated string literals frozen? — bughitgithub@...

Issue #17104 has been reported by bughit (bug hit).

23 messages 2020/08/05

[#99499] [Ruby master Bug#17105] A single `return` can return to two different places in a proc inside a lambda inside a method — eregontp@...

Issue #17105 has been reported by Eregon (Benoit Daloze).

10 messages 2020/08/06

[#99582] [Ruby master Feature#17122] Add category to Warning#warn — eileencodes@...

Issue #17122 has been reported by eileencodes (Eileen Uchitelle).

20 messages 2020/08/13

[#99700] [Ruby master Bug#17129] bundle install `eventmachine` and `sassc` fails since 914b2208ab3eddec478cdc3e079e6c30d0f0892c — yasuo.honda@...

SXNzdWUgIzE3MTI5IGhhcyBiZWVuIHJlcG9ydGVkIGJ5IHlhaG9uZGEgKFlhc3VvIEhvbmRhKS4N

9 messages 2020/08/26

[ruby-core:99778] [Ruby master Feature#13683] Add strict Enumerable#single

From: eregontp@...
Date: 2020-08-30 11:26:25 UTC
List: ruby-core #99778
Issue #13683 has been updated by Eregon (Benoit Daloze).


matz (Yukihiro Matsumoto) wrote in #note-10:
> Hmm, I don't like the name `single`.

matz (Yukihiro Matsumoto) wrote in #note-17:
> I don't like `only` either since these names do not describe the behavior.

Could you explain why?
I think `single` as in "return a single element or error out" is the best name here, and the most intuitive.
If I want a random element, then #sample already exists, so I don't think there would be any confusion to what `Array#single` or `Enumerable#single` would do.

`array.only` reads weirdly to me. `array.single` is so much nicer, no?

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

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

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next