Sha256: 065f8fac43b2100d6ec48f3842a597fb733066a2fcb86083bbe5222886af114c

Contents?: true

Size: 623 Bytes

Versions: 5

Compression:

Stored size: 623 Bytes

Contents

module Enumerable

  # A more versitle #compact method. It can be used to
  # collect and filter items out in one single step.
  #
  #   [1,2,3].compact_map do |n|
  #     n < 1 ? nil : n
  #   end
  #
  # _produces_
  #
  #   [2,3]
  #
  # NOTE: Perhaps nicer to have as added functionality for #compact.
  #
  # CREDIT: Trans

  def compact_map(trash=nil, &block)
    y = []
    if block_given?
      each do |*a|
        r = yield(*a)
        y << r unless trash == r
      end
    else
      each do |r|
        y << r unless trash == r
      end
    end
    y
  end

  alias_method :compact_collect, :compact_map
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
facets-2.8.0 lib/core/facets/enumerable/compact_map.rb
facets-2.7.0 lib/core/facets/enumerable/compact_map.rb
facets-2.6.0 lib/core/facets/enumerable/compact_map.rb
facets-2.5.1 lib/core/facets/enumerable/compact_map.rb
facets-2.5.2 lib/core/facets/enumerable/compact_map.rb