Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Array#select!

array.select! {|element| ... } → self or nil
array.select! → new_enumerator

Calls the block, if given with each element of self; removes from self those elements for which the block returns false or nil.

Returns self if any elements were removed:

Example
a = [:foo, 'bar', 2, :bam]
a.select! {|element| element.to_s.start_with?('b') } # => ["bar", :bam]

Returns nil if no elements were removed.

Returns a new Enumerator if no block given:

Example
a = [:foo, 'bar', 2, :bam]
a.select! # => #<Enumerator: [:foo, "bar", 2, :bam]:select!>

Array#filter! is an alias for Array#select!.

See also #select #shelljoin #sample #shift also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1