[ruby-core:105818] [Ruby master Feature#16252] Hash#partition should return hashes
From:
"zverok (Victor Shepelev)" <noreply@...>
Date:
2021-10-27 07:03:36 UTC
List:
ruby-core #105818
Issue #16252 has been updated by zverok (Victor Shepelev).
I am afraid we don't have some particular rules for Enumerables to return their own class:
```ruby
Set[*1..8].select(&:odd?)
# => [1, 3, 5, 7] -- not a Set
```
Making `Hash#select` and `#reject` return hashes was an ad-hoc decision (and quite useful at that!); maybe it would be good to consistently follow this decision for some core collections?
FWIF, I once [made a gem](https://github.com/zverok/idempotent_enumerable) to do that for any collection of your liking, and while working on it, I identified lists of methods where it is probably desirable to return same class: [methods returning singular collection](https://github.com/zverok/idempotent_enumerable#methods-that-return-single-collection) and [methods returning several collections](https://github.com/zverok/idempotent_enumerable#methods-that-return-or-emit-several-collections).
----------------------------------------
Feature #16252: Hash#partition should return hashes
https://bugs.ruby-lang.org/issues/16252#change-94342
* Author: Dan0042 (Daniel DeLorme)
* Status: Open
* Priority: Normal
----------------------------------------
Hash#partition is implemented by Enumerable so it just returns two arrays of arrays
```ruby
{1=>2,3=>4}.partition{|k,|k==1} #=> [[[1, 2]], [[3, 4]]]
```
But I think it would make more sense to behave similarly to Hash#select and Hash#reject
```ruby
{1=>2,3=>4}.partition{|k,|k==1} #=> [{1=>2}, {3=>4}]
```
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>