Actions
Feature #17208
openAdd `Set#compact` and `Set#compact!` methods
Status:
Open
Assignee:
-
Target version:
-
Description
This is a proposal to add compact
and compact!
methods already owned by Array
and Hash
to Set
.
-
Array#compact
andArray#compact!
has been around for a long time. -
Hash#compact
hasHash#compact!
been added since Ruby 2.4 ... https://bugs.ruby-lang.org/issues/11818 - There is
Set
in collection libraries other thanArray
andHash
. ButSet
doesn't havecompact
andcompact!
methods.
It behaves the same as compact
and compact!
methods of Array
and Hash
as follows.
Set#compact!
:
# Removes all nil elements from self. Returns self if any elements removed, otherwise nil.
set = Set[1, 'c', nil] #=> #<Set: {1, "c", nil}>
set.compact! #=> #<Set: {1, "c"}>
set #=> #<Set: {1, "c"}>
set = Set[1, 'c'] #=> #<Set: {1, "c"}>
set.compact! #=> nil
set #=> #<Set: {1, "c"}>
Set#compact
:
# Returns a new Set containing all non-nil elements from self.
set = Set[1, 'c', nil] #=> #<Set: {1, "c", nil}>
set.compact #=> #<Set: {1, "c"}>
set #=> #<Set: {1, "c", nil}>
set = Set[1, 'c'] #=> #<Set: {1, "c"}>
set.compact #=> #<Set: {1, "c"}>
set #=> #<Set: {1, "c"}>
Pull Request ... https://github.com/ruby/ruby/pull/3617
Actions
Like0
Like0Like0Like0Like0Like0