Sha256: 42eb840a14407160bde60ac953cd2484333d844990786dd670a2c44c179bc59a

Contents?: true

Size: 1005 Bytes

Versions: 22

Compression:

Stored size: 1005 Bytes

Contents

#--
# Credit goes to Florian Gross.
#++
require 'facet/enumerable/filter_collect'

module Enumerable

  # Collects/Maps and compacts items in one single step.
  # The items for which the supplied block returns +nil+ will not
  # end up in the resulting array.
  #
  #   # Return telephone numbers of all persons that have a telephone number.
  #   persons.compact_collect { |person| person.telephone_no }
  #
  # Also see Enumerable#collect, Enumerable#map, Array#compact.
  #
  def compact_collect #:yield:
    filter_collect do |item|
      new_item = yield(item)
      throw(:skip) if new_item.nil?
      new_item
    end
  end

  alias_method :compact_map, :compact_collect

end



#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCEnumerable < Test::Unit::TestCase

    def test_compact_collect
      a = [1,2,nil,4].compact_collect { |e| e }
      assert_equal( [1,2,4], a )
    end

  end

=end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
facets-1.0.0 lib/facet/enumerable/compact_collect.rb
facets-1.0.3 packages/core/lib/facet/enumerable/compact_collect.rb
facets-1.2.1 lib/facets/core/enumerable/compact_collect.rb
facets-1.3.0 lib/facets/core/enumerable/compact_collect.rb
facets-1.1.0 lib/facet/enumerable/compact_collect.rb
facets-1.2.0 lib/facets/core/enumerable/compact_collect.rb
facets-1.3.1 lib/facets/core/enumerable/compact_collect.rb
facets-1.3.3 lib/facets/core/enumerable/compact_collect.rb
facets-1.3.2 lib/facets/core/enumerable/compact_collect.rb
facets-1.4.0 lib/facets/core/enumerable/compact_collect.rb
facets-1.4.1 lib/facets/core/enumerable/compact_collect.rb
facets-1.4.2 lib/facets/core/enumerable/compact_collect.rb
facets-1.4.3 lib/facets/core/enumerable/compact_collect.rb
facets-1.4.5 lib/facets/core/enumerable/compact_collect.rb
facets-1.4.4 lib/facets/core/enumerable/compact_collect.rb
facets-1.7.30 lib/facets/core/enumerable/compact_collect.rb
facets-1.7.0 lib/facets/core/enumerable/compact_collect.rb
facets-1.7.38 lib/facets/core/enumerable/compact_collect.rb
facets-1.7.46 lib/facets/core/enumerable/compact_collect.rb
facets-1.8.0 lib/facets/core/enumerable/compact_collect.rb