Skip to content

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

instance method · added in Ruby 3.1

Enumerable#compact

compact → array

Returns an array of all non-nil elements:

Example
a = [nil, 0, nil, 'a', false, nil, false, nil, 'a', nil, 0, nil]
a.compact # => [0, "a", false, false, "a", 0]

Ruby version changes

  1. 3.1
See also #collect_concat #count #collect #cycle also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1