Sha256: 8def1e08f1073f9a1f0e7379e708a1f893f23d1335e6ec81282763c530ed760d

Contents?: true

Size: 550 Bytes

Versions: 1

Compression:

Stored size: 550 Bytes

Contents

module Enumerable

  # A more versitle #compact method. It can be used to
  # collect and filter items out in one single step.
  #
  #   (1..3).compact_map do |n|
  #     n < 1 ? nil : n
  #   end
  #
  # _produces_
  #
  #   [2,3]
  #
  # 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

1 entries across 1 versions & 1 rubygems

Version Path
facets-2.8.1 lib/core/facets/enumerable/compact_map.rb