Actions
Feature #21791
openImplement Set#compact/Set#compact!, these should return Set instead of Array
Feature #21791:
Implement Set#compact/Set#compact!, these should return Set instead of Array
Status:
Open
Assignee:
-
Target version:
-
Description
I recently had to remove a nil value from a Set, and ended up with an Array:
irb(main):001> Set[1, 2, nil, 3].compact
=> [1, 2, 3]
irb(main):002> Set[1, 2, nil, 3].compact.class
=> Array
Since there is no dedicated Set#compact, this is done via Enumerable#compact and this results in an Array. To preserve the Set, the following works:
set - [nil] # compact
set.delete_if(&:nil?) # compact!
set.compact.to_set # compact, but slow
Both are rather ugly.
This patch implements Set#compact and Set#compact! in a way that preserves the class.
There are probably more methods that could have their own implementation, for example Set#select/Set#reject now returns arrays too (but Set#select! and Set#reject! work as expected.
Pull request: https://github.com/ruby/ruby/pull/15614
Actions