instance method compact_blank

Ruby on Rails 6.1.7.10

Since v6.1.7.10

Available in: v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

compact_blank()

Returns a new Array without the blank items. Uses Object#blank? for determining if an item is blank.

[1, "", nil, 2, " ", [], {}, false, true].compact_blank
# =>  [1, 2, true]

Set.new([nil, "", 1, 2])
# => [2, 1] (or [1, 2])

When called on a Hash, returns a new Hash without the blank values.

{ a: "", b: 1, c: nil, d: [], e: false, f: true }.compact_blank
#=> { b: 1, f: true }
Source
# File activesupport/lib/active_support/core_ext/enumerable.rb, line 191
  def compact_blank
    reject(&:blank?)
  end

Defined in activesupport/lib/active_support/core_ext/enumerable.rb line 191 · View on GitHub · Improve this page · Find usages on GitHub

Defined in Enumerable

Type at least 2 characters to search.

↑↓ navigate · open · esc close