From: "Eregon (Benoit Daloze) via ruby-core" Date: 2026-01-07T12:08:38+00:00 Subject: [ruby-core:124432] [Ruby Feature#13683] Add strict Enumerable#single Issue #13683 has been updated by Eregon (Benoit Daloze). +1 for `sole` (given Matz didn't like `single` and `only`). It's clear and obvious what it does. We could also allow it to take a block to address #18135 too: ```ruby enumerable.sole { condition(it) } # same as enumerable.select { condition(it) }.sole # but more efficient (avoids Array allocation & just raise when condition matches 2 elements vs executing it on remaining elements needlessly) ``` --- `one` sounds find too, and seems consistent with the existing `one?`. @nobu had a concern in https://bugs.ruby-lang.org/issues/13683#note-30 but I am not sure I understand it: > It looks close to `Enumerable#one?` which counts truthy values only, but has a different semantics. I guess it's about the form without a block `enum.one` vs `enum.one?`, `[nil].one # => nil (= success)` vs `[false].one? # => false (= failure)`. It's a good point, that would be confusing (because `one?` has a "default block" of `{ it }` while this feature would have a "default block" of `{ true }`). > If collection is empty and a block was given, returns the block's return value: I wouldn't expect that, the block if any is accepted would be for `find/select`, not for default value. "single" etc is an assertion there is a single element matching something, it should always raise if empty or no matching element. ---------------------------------------- Feature #13683: Add strict Enumerable#single https://bugs.ruby-lang.org/issues/13683#change-115981 * Author: dnagir (Dmytrii Nagirniak) * Status: Feedback ---------------------------------------- ### 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/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/