Module: Enumerable

### Home Classes Methods

### In Files

- lib/backports/1.8.7/enumerable/count.rb
- lib/backports/1.8.7/enumerable/cycle.rb
- lib/backports/1.8.7/enumerable/drop.rb
- lib/backports/1.8.7/enumerable/drop_while.rb
- lib/backports/1.8.7/enumerable/entries.rb
- lib/backports/1.8.7/enumerable/find_index.rb
- lib/backports/1.8.7/enumerable/first.rb
- lib/backports/1.8.7/enumerable/group_by.rb
- lib/backports/1.8.7/enumerable/inject.rb
- lib/backports/1.8.7/enumerable/max_by.rb
- lib/backports/1.8.7/enumerable/min_by.rb
- lib/backports/1.8.7/enumerable/minmax.rb
- lib/backports/1.8.7/enumerable/minmax_by.rb
- lib/backports/1.8.7/enumerable/none.rb
- lib/backports/1.8.7/enumerable/one.rb
- lib/backports/1.8.7/enumerable/reverse_each.rb
- lib/backports/1.8.7/enumerable/take.rb
- lib/backports/1.8.7/enumerable/take_while.rb
- lib/backports/1.8.7/enumerable/to_a.rb
- lib/backports/1.9.1/enumerable/each_with_index.rb
- lib/backports/1.9.1/enumerable/each_with_object.rb
- lib/backports/1.9.2/enumerable/chunk.rb
- lib/backports/1.9.2/enumerable/each_entry.rb
- lib/backports/1.9.2/enumerable/flat_map.rb
- lib/backports/1.9.2/enumerable/slice_before.rb
- lib/backports/2.0.0/enumerable/lazy.rb
- lib/backports/rails/enumerable.rb

### Namespace

- CLASS Enumerable::Enumerator

### Methods

- #chunk
- #collect_concat
- #count
- #cycle
- #drop
- #drop_while
- #each_entry
- #each_with_index_with_optional_args_and_block
- #each_with_object
- #entries_with_optional_arguments
- #find_index
- #first
- #flat_map
- #group_by
- #inject_with_symbol
- #lazy
- #max_by
- #min_by
- #minmax
- #minmax_by
- #none?
- #one?
- #reverse_each
- #slice_before
- #sum
- #take
- #take_while
- #to_a_with_optional_arguments

### Class/Module Index

Quicksearch

- Prime
- Prime::EratosthenesGenerator
- Prime::EratosthenesSieve
- Prime::Generator23
- Prime::OldCompatibility
- Prime::PseudoPrimeGenerator
- Prime::TrialDivision
- Prime::TrialDivisionGenerator
- Backports
- Backports::MOST_EXTREME_OBJECT_EVER
- Backports::StdLib
- Backports::StdLib::LoadedFeatures
- Enumerator
- Enumerator::GeneratorBP
- Enumerator::Lazy
- Enumerator::Yielder
- Matrix
- Matrix::EigenvalueDecomposition
- Matrix::LUPDecomposition
- Random
- Random::Implementation
- Random::MT19937
- Enumerable
- Enumerable::Enumerator
- File
- File::Constants
- ARGF
- Array
- BasicObject
- Binding
- Complex
- Dir
- ENV
- Fixnum
- Float
- GC
- Hash
- IO
- Integer
- Kernel
- Math
- Method
- Module
- NilClass
- Numeric
- Object
- ObjectSpace
- OpenStruct
- Proc
- Process
- Range
- Regexp
- Set
- StopIteration
- String
- Struct
- Symbol
- UnboundMethod

No matching classes.

# Enumerable

### Public Instance Methods

chunk(initial_state = nil, &original_block) click to toggle source

    # File lib/backports/1.9.2/enumerable/chunk.rb, line 5
    def chunk(initial_state = nil, &original_block)
      raise ArgumentError, "no block given" unless block_given?
      ::Enumerator.new do |yielder|
        previous = nil
        accumulate = []
        block = initial_state.nil? ? original_block : Proc.new{|val| original_block.call(val, initial_state.clone)}
        each do |val|
          key = block.call(val)
          if key.nil? || (key.is_a?(Symbol) && key.to_s[0,1] == "_")
            yielder.yield [previous, accumulate] unless accumulate.empty?
            accumulate = []
            previous = nil
            case key
            when nil, :_separator
            when :_alone
              yielder.yield [key, [val]]
            else
              raise RuntimeError, "symbol beginning with an underscore are reserved"
            end
          else
            if previous.nil? || previous == key
              accumulate << val
            else
              yielder.yield [previous, accumulate] unless accumulate.empty?
              accumulate = [val]
            end
            previous = key
          end
        end
        # what to do in case of a break?
        yielder.yield [previous, accumulate] unless accumulate.empty?
      end
    end

collect_concat(&block) click to toggle source
Alias for: flat_map
count(item = Backports::Undefined) click to toggle source

    # File lib/backports/1.8.7/enumerable/count.rb, line 5
    def count(item = Backports::Undefined)
      seq = 0
      if item != Backports::Undefined
        each { |o| seq += 1 if item == o }
      elsif block_given?
        each { |o| seq += 1 if yield(o) }
      else
        each { seq += 1 }
      end
      seq
    end

cycle(n = nil) click to toggle source

    # File lib/backports/1.8.7/enumerable/cycle.rb, line 6
    def cycle(n = nil)
      return to_enum(:cycle, n) unless block_given?
      n = n && Backports.coerce_to_int(n)
      if n == nil || n >= 1
        cache = []
        each do |elem|
          cache << elem
          yield elem
        end
        if n
          (n-1).times { cache.each{|e| yield e } }
        else
          loop        { cache.each{|e| yield e } }
        end unless cache.empty?
      end
      nil
    end

drop(n) click to toggle source

    # File lib/backports/1.8.7/enumerable/drop.rb, line 5
    def drop(n)
      n = Backports.coerce_to_int(n)
      raise ArgumentError, "attempt to drop negative size" if n < 0
      ary = to_a
      return [] if n > ary.size
      ary[n...ary.size]
    end

drop_while() click to toggle source

Standard in Ruby 1.8.7+. See official documentation

    # File lib/backports/1.8.7/enumerable/drop_while.rb, line 6
    def drop_while
      return to_enum(:drop_while) unless block_given?
      ary = []
      dropping = true
      each do |obj|
        ary << obj unless dropping &&= yield(obj)
      end
      ary
    end

each_entry(*pass) click to toggle source

    # File lib/backports/1.9.2/enumerable/each_entry.rb, line 3
    def each_entry(*pass)
      return to_enum(:each_entry, *pass) unless block_given?
      each(*pass) do |*args|
        yield args.size == 1 ? args[0] : args
      end
      self
    end

each_with_index_with_optional_args_and_block(*args) click to toggle source

    # File lib/backports/1.9.1/enumerable/each_with_index.rb, line 6
    def each_with_index_with_optional_args_and_block(*args)
      return to_enum(:each_with_index, *args) unless block_given?
      idx = 0
      each(*args) { |o| yield(o, idx); idx += 1 }
      self
    end

each_with_object(memo) click to toggle source

    # File lib/backports/1.9.1/enumerable/each_with_object.rb, line 3
    def each_with_object(memo)
      return to_enum(:each_with_object, memo) unless block_given?
      each {|obj| yield obj, memo}
      memo
    end

entries_with_optional_arguments(*args) click to toggle source

    # File lib/backports/1.8.7/enumerable/entries.rb, line 5
    def entries_with_optional_arguments(*args)
      return entries_without_optional_arguments if args.empty?
      to_enum(:each, *args).entries
    end

find_index(obj = Backports::Undefined) click to toggle source

    # File lib/backports/1.8.7/enumerable/find_index.rb, line 6
    def find_index(obj = Backports::Undefined)
      if obj != Backports::Undefined
        each_with_index do |element, i|
          return i if element == obj
        end
      elsif block_given?
        each_with_index do |element, i|
          return i if yield element
        end
      else
        return to_enum(:find_index)
      end
      nil
    end

first(n = Backports::Undefined) click to toggle source

    # File lib/backports/1.8.7/enumerable/first.rb, line 5
    def first(n = Backports::Undefined)
      if n == Backports::Undefined
        each{|obj| return obj}
        nil
      else
        n = Backports.coerce_to_int(n)
        raise ArgumentError, "attempt to take negative size: #{n}" if n < 0
        array = []
        each do |elem|
          array << elem
          break if array.size >= n
        end unless n == 0
        array
      end
    end

flat_map(&block) click to toggle source

    # File lib/backports/1.9.2/enumerable/flat_map.rb, line 5
    def flat_map(&block)
      return to_enum(:flat_map) unless block_given?
      map(&block).flatten(1)
    end

Also aliased as: collect_concat
group_by() click to toggle source

    # File lib/backports/1.8.7/enumerable/group_by.rb, line 5
    def group_by
      return to_enum(:group_by) unless block_given?
      result = {}
      each do |o|
        result.fetch(yield(o)){|key| result[key] = []} << o
      end
      result
    end

inject_with_symbol(*args, &block) click to toggle source

    # File lib/backports/1.8.7/enumerable/inject.rb, line 5
    def inject_with_symbol(*args, &block)
      return inject_without_symbol(*args, &block) if block_given? && args.size <= 1
      method = args.pop
      inject_without_symbol(*args) {|memo, obj| memo.send(method, obj)}
    end

lazy() click to toggle source

    # File lib/backports/2.0.0/enumerable/lazy.rb, line 6
    def lazy
      klass = Enumerator::Lazy.send :class_variable_get, :@@lazy_with_no_block # Note: class_variable_get is private in 1.8
      Enumerator::Lazy.new(klass.new(self, :each, []))
    end

max_by() click to toggle source

    # File lib/backports/1.8.7/enumerable/max_by.rb, line 6
    def max_by
      return to_enum(:max_by) unless block_given?
      max_object, max_result = nil, Backports::MOST_EXTREME_OBJECT_EVER
      each do |object|
        result = yield object
        max_object, max_result = object, result if max_result < result
      end
      max_object
    end

min_by() click to toggle source

    # File lib/backports/1.8.7/enumerable/min_by.rb, line 6
    def min_by
      return to_enum(:min_by) unless block_given?
      min_object, min_result = nil, Backports::MOST_EXTREME_OBJECT_EVER
      each do |object|
        result = yield object
        min_object, min_result = object, result if min_result > result
      end
      min_object
    end

minmax() click to toggle source

    # File lib/backports/1.8.7/enumerable/minmax.rb, line 5
    def minmax
      return minmax{|a,b| a <=> b} unless block_given?
      first_time = true
      min, max = nil
      each do |object|
        if first_time
          min = max = object
          first_time = false
        else
          min = object if Backports.coerce_to_comparison(min, object, yield(min, object)) > 0
          max = object if Backports.coerce_to_comparison(max, object, yield(max, object)) < 0
        end
      end
      [min, max]
    end

minmax_by() click to toggle source

Standard in Ruby 1.8.7+. See official documentation

    # File lib/backports/1.8.7/enumerable/minmax_by.rb, line 7
    def minmax_by
      return to_enum(:minmax_by) unless block_given?
      min_object, min_result = nil, Backports::MOST_EXTREME_OBJECT_EVER
      max_object, max_result = nil, Backports::MOST_EXTREME_OBJECT_EVER
      each do |object|
        result = yield object
        min_object, min_result = object, result if min_result > result
        max_object, max_result = object, result if max_result < result
      end
      [min_object, max_object]
    end

none?(&block) click to toggle source

    # File lib/backports/1.8.7/enumerable/none.rb, line 3
    def none?(&block)
      !any?(&block)
    end

one?() click to toggle source

    # File lib/backports/1.8.7/enumerable/one.rb, line 3
    def one?
      found_one = false
      if block_given?
        each do |o|
          if yield(o)
            return false if found_one
            found_one = true
          end
        end
      else
        each do |o|
          if o
            return false if found_one
            found_one = true
          end
        end
      end
      found_one
    end

reverse_each() click to toggle source

    # File lib/backports/1.8.7/enumerable/reverse_each.rb, line 5
    def reverse_each
      return to_enum(:reverse_each) unless block_given?
      # There is no other way then to convert to an array first... see 1.9's source.
      to_a.reverse_each{|e| yield e}
      self
    end

slice_before(arg = Backports::Undefined, &block) click to toggle source

    # File lib/backports/1.9.2/enumerable/slice_before.rb, line 6
    def slice_before(arg = Backports::Undefined, &block)
      if block_given?
        has_init = !(arg.equal? Backports::Undefined)
      else
        raise ArgumentError, "wrong number of arguments (0 for 1)" if arg.equal? Backports::Undefined
        block = Proc.new{|elem| arg === elem }
      end
      Enumerator.new do |yielder|
        init = arg.dup if has_init
        accumulator = nil
        each do |elem|
          start_new = has_init ? block.call(elem, init) : block.call(elem)
          if start_new
            yielder.yield accumulator if accumulator
            accumulator = [elem]
          else
            accumulator ||= []
            accumulator << elem
          end
        end
        yielder.yield accumulator if accumulator
      end
    end

sum(identity = 0, &block) click to toggle source

Standard in rails... See official documentation Modified from rails 2.3 to not rely on size

    # File lib/backports/rails/enumerable.rb, line 4
    def sum(identity = 0, &block)
      if block_given?
        map(&block).sum(identity)
      else
        inject { |sum, element| sum + element } || identity
      end
    end

take(n) click to toggle source

    # File lib/backports/1.8.7/enumerable/take.rb, line 5
    def take(n)
      first(n)
    end

take_while() click to toggle source

Standard in Ruby 1.8.7+. See official documentation

    # File lib/backports/1.8.7/enumerable/take_while.rb, line 6
    def take_while
      return to_enum(:take_while) unless block_given?
      inject([]) do |array, elem|
        return array unless yield elem
        array << elem
      end
    end

to_a_with_optional_arguments(*args) click to toggle source

    # File lib/backports/1.8.7/enumerable/to_a.rb, line 6
    def to_a_with_optional_arguments(*args)
      return to_a_without_optional_arguments if args.empty?
      to_enum(:each, *args).to_a
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.