Sha256: 6f95e2bc4542d4d94537cac559f7e6cefbab63a215239703e1cb76cc0bb14814

Contents?: true

Size: 726 Bytes

Versions: 1

Compression:

Stored size: 726 Bytes

Contents

#
class Integer

  # Like #times but returns a collection of the yield results.
  #
  #   a = 3.of { |i| "#{i+1}" }
  #   a => [ "1", "2", "3" ]
  #
  def of(&yld)
    a = []; self.times{ |i| a << yld.call(i) }
    a
  end

  alias_method :times_collect, :of
  alias_method :times_map, :of

end




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

  require 'test/unit'

  class TestIntegerEnumerate < Test::Unit::TestCase

    def test_of
      a = 4
      b = a.of{ |i| i*2 }
      assert_equal( [0,2,4,6], b )
    end

    def test_times_collect
      a = 4
      b = a.times_collect{ |i| i*2 }
      assert_equal( [0,2,4,6], b )
    end

  end

=end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
facets-2.0.0 lib/core/facets/integer/of.rb