Sha256: 881adb31d3992b2f4cb5c89cc1eeb53bd99548412c1888055e1fb3f74f3ec579
Contents?: true
Size: 1.09 KB
Versions: 26
Compression:
Stored size: 1.09 KB
Contents
#-- # Credit goes to Florian Gross. #++ module Enumerable # Collects/Maps and filters items out in one single step. # You can use throw(:skip) in the supplied block to indicate that the # current item should not end up in the resulting array. # # # Return names of all person with an age of at least 18. # persons.filter_collect do |person| # throw(:skip) if person.age < 18 # person.name # end # # Also see Enumerable#collect, Enumerable#find_all. # def filter_collect #:yield: result = [] self.each do |item| catch(:skip) do new_item = yield(item) result << new_item end end return result end alias_method :filter_map, :filter_collect end #Enumerable # _____ _ # |_ _|__ ___| |_ # | |/ _ \/ __| __| # | | __/\__ \ |_ # |_|\___||___/\__| # =begin test require 'test/unit' class TCEnumerable < Test::Unit::TestCase def test_filter_collect e = [3,4] a = [1,2,3,4].filter_collect { |n| throw(:skip) if n < 3 n } assert_equal( e, a ) end end =end
Version data entries
26 entries across 26 versions & 1 rubygems