Sha256: 89682f74516f1943d01caf13d148b4c80af8a632b36fbbdf19814c7e47b97625

Contents?: true

Size: 662 Bytes

Versions: 10

Compression:

Stored size: 662 Bytes

Contents

module Enumerable

  # A more versitle #compact method. It can be used to
  # collect and filter items out in one single step.
  #
  #   c = [1,2,3].compact_map do |n|
  #     n < 2 ? nil : n
  #   end
  #
  #   c  #=> [2,3]
  #
  # CREDIT: Trans
  #
  # DEPRECATE: This method should probably be removed b/c #purge
  # does almost the same thing and enum.map{}.compact works too.

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

  #
  alias_method :compact_collect, :compact_map

end

Version data entries

10 entries across 9 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/enumerable/compact_map.rb
facets-3.1.0 lib/core/facets/enumerable/compact_map.rb
facets-3.0.0 lib/core/facets/enumerable/compact_map.rb
facets-2.9.3 lib/core/facets/enumerable/compact_map.rb
facets-2.9.2 lib/core/facets/enumerable/compact_map.rb
facets-2.9.2 src/core/facets/enumerable/compact_map.rb
facets-2.9.1 lib/core/facets/enumerable/compact_map.rb
facets-2.9.0 lib/core/facets/enumerable/compact_map.rb
facets-2.9.0.pre.2 lib/core/facets/enumerable/compact_map.rb
facets-2.9.0.pre.1 lib/core/facets/enumerable/compact_map.rb