Enumerable

Public Instance Methods

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

Standard in Ruby 1.9.2 See official documentation

# File lib/backports/1.9.2/enumerable.rb, line 3
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.yield(val, initial_state.clone)}
    each do |val|
      key = block.yield(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 :_singleton
          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
count(item = Backports::Undefined) click to toggle source

Standard in Ruby 1.8.7+. See official documentation

# File lib/backports/1.8.7/enumerable.rb, line 4
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

Standard in Ruby 1.8.7+. See official documentation

# File lib/backports/1.8.7/enumerable.rb, line 17
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

Standard in Ruby 1.8.7+. See official documentation

# File lib/backports/1.8.7/enumerable.rb, line 41
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.rb, line 50
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.rb, line 37
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.8.7/enumerable.rb, line 62
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

Standard in Ruby 1.8.8. See official documentation

# File lib/backports/1.9.1/enumerable.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.rb, line 245
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

Standard in Ruby 1.8.7+. See official documentation

# File lib/backports/1.8.7/enumerable.rb, line 72
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

Standard in Ruby 1.8.7+. See official documentation

# File lib/backports/1.8.7/enumerable.rb, line 88
def first(n = Backports::Undefined)
  return take(n) unless n == Backports::Undefined
  each{|obj| return obj}
  nil
end
flat_map(&block) click to toggle source
# File lib/backports/1.9.2/enumerable.rb, line 45
def flat_map(&block)
  return to_enum(:flat_map) unless block_given?
  map(&block).flatten(1)
end
group_by() click to toggle source

Standard in Ruby 1.8.7+. See official documentation

# File lib/backports/1.8.7/enumerable.rb, line 95
def group_by
  return to_enum(:group_by) unless block_given?
  {}.tap do |result|
    each do |o|
      result.fetch(yield(o)){|key| result[key] = []} << o
    end
  end
end
inject_with_symbol(*args, &block) click to toggle source
# File lib/backports/1.8.7/enumerable.rb, line 106
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.rb, line 2
def lazy
  Enumerator::Lazy.new(self){|yielder, *val| yielder.<<(*val)}
end
max_by() click to toggle source

Standard in Ruby 1.8.7+. See official documentation

# File lib/backports/1.8.7/enumerable.rb, line 126
def max_by
  return to_enum(:max_by) unless block_given?
  max_object, max_result = nil, 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

Standard in Ruby 1.8.7+. See official documentation

# File lib/backports/1.8.7/enumerable.rb, line 137
def min_by
  return to_enum(:min_by) unless block_given?
  min_object, min_result = nil, 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

Standard in Ruby 1.8.7+. See official documentation

# File lib/backports/1.8.7/enumerable.rb, line 148
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.rb, line 165
def minmax_by
  return to_enum(:minmax_by) unless block_given?
  min_object, min_result = nil, MOST_EXTREME_OBJECT_EVER
  max_object, max_result = nil, 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

Standard in Ruby 1.8.7+. See official documentation

# File lib/backports/1.8.7/enumerable.rb, line 178
def none?(&block)
  !any?(&block)
end
one?() click to toggle source

Standard in Ruby 1.8.7+. See official documentation

# File lib/backports/1.8.7/enumerable.rb, line 183
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

Standard in Ruby 1.8.7+. See official documentation

# File lib/backports/1.8.7/enumerable.rb, line 206
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.rb, line 51
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.yield(elem, init) : block.yield(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

Standard in Ruby 1.8.7+. See official documentation

# File lib/backports/1.8.7/enumerable.rb, line 214
def take(n)
  n = Backports.coerce_to_int(n)
  raise ArgumentError, "attempt to take negative size: #{n}" if n < 0
  [].tap do |array|
    each do |elem|
      array << elem
      break if array.size >= n
    end unless n <= 0
  end
end
take_while() click to toggle source

Standard in Ruby 1.8.7+. See official documentation

# File lib/backports/1.8.7/enumerable.rb, line 226
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.rb, line 236
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.