module Enumerable

Public Instance Methods

all_with_pattern?(pattern = Backports::Undefined, &block) click to toggle source
# File lib/backports/2.5.0/enumerable/all.rb, line 6
def all_with_pattern?(pattern = Backports::Undefined, &block)
  return all_without_pattern?(&block) if Backports::Undefined == pattern
  each_entry { |x| return false unless pattern === x }
  true
end
any_with_pattern?(pattern = Backports::Undefined, &block) click to toggle source
# File lib/backports/2.5.0/enumerable/any.rb, line 6
def any_with_pattern?(pattern = Backports::Undefined, &block)
  return any_without_pattern?(&block) if Backports::Undefined == pattern
  each_entry { |x| return true if pattern === x }
  false
end
chain(*enums) click to toggle source
# File lib/backports/2.6.0/enumerable/chain.rb, line 3
def chain(*enums)
  Enumerator::Chain.new(self, *enums)
end
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
chunk_while(&block) click to toggle source
# File lib/backports/2.3.0/enumerable/chunk_while.rb, line 6
def chunk_while(&block)
  raise ArgumentError, 'tried to create Proc object without a block' unless block
  enum = self
  Enumerator.new do |y|
    acc = []
    prev = Backports::Undefined
    enum.each do |*elem|
      elem = elem.first if elem.length == 1
      unless prev == Backports::Undefined
        unless block.call(prev, elem)
          y.yield acc
          acc = []
        end
      end
      acc << elem
      prev = elem
    end
    y.yield acc unless acc.empty?
  end
end
collect_concat()
Alias for: flat_map
count(item = Backports::Undefined) { |o| ... } 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) { |elem| ... } 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() { |obj| ... } 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) { |size == 1 ? args : args| ... } 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) { |o, idx| ... } 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) { |obj, 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) { |element| ... } 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() { |*args| ... } click to toggle source
# File lib/backports/1.9.2/enumerable/flat_map.rb, line 3
def flat_map
  return to_enum(:flat_map) unless block_given?
  r = []
  each do |*args|
    result = yield(*args)
    result.respond_to?(:to_ary) ? r.concat(result) : r.push(result)
  end
  r
end
Also aliased as: collect_concat
grep_v(pattern) { |v| ... } click to toggle source
# File lib/backports/2.3.0/enumerable/grep_v.rb, line 5
def grep_v(pattern)
  if block_given?
    acc = []
    each_entry do |v|
      acc << yield(v) unless pattern === v
    end
    acc
  else
    reject {|v| pattern === v }
  end
end
group_by() { |o| ... } 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 6
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() { |object| ... } 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() { |object| ... } 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() { |min, object| ... } 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() { |object| ... } 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
none_with_pattern?(pattern = Backports::Undefined, &block) click to toggle source
# File lib/backports/2.5.0/enumerable/none.rb, line 8
def none_with_pattern?(pattern = Backports::Undefined, &block)
  return none_without_pattern?(&block) if Backports::Undefined == pattern
  each_entry { |x| return false if pattern === x }
  true
end
one?() { |o| ... } 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
one_with_pattern?(pattern = Backports::Undefined, &block) click to toggle source
# File lib/backports/2.5.0/enumerable/one.rb, line 8
def one_with_pattern?(pattern = Backports::Undefined, &block)
  return one_without_pattern?(&block) if Backports::Undefined == pattern
  found_one = false
  each_entry do |o|
    if pattern === o
      return false if found_one
      found_one = true
    end
  end
  found_one
end
reverse_each() { |e| ... } 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_after(pattern = Backports::Undefined, &block) click to toggle source
# File lib/backports/2.2.0/enumerable/slice_after.rb, line 6
def slice_after(pattern = Backports::Undefined, &block)
  raise ArgumentError, 'both pattern and block are given' if pattern != Backports::Undefined && block
  raise ArgumentError, 'wrong number of arguments (given 0, expected 1)' if pattern == Backports::Undefined && !block
  enum = self
  block ||= Proc.new{|elem| pattern === elem}
  Enumerator.new do |y|
    acc = []
    enum.each do |*elem|
      elem = elem.first if elem.length == 1
      acc << elem
      if block.call(elem)
        y.yield acc
        acc = []
      end
    end
    y.yield acc unless acc.empty?
  end
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
slice_when(&block) click to toggle source
# File lib/backports/2.2.0/enumerable/slice_when.rb, line 6
def slice_when(&block)
  raise ArgumentError, 'tried to create Proc object without a block' unless block
  enum = self
  Enumerator.new do |y|
    acc = []
    prev = Backports::Undefined
    enum.each do |*elem|
      elem = elem.first if elem.length == 1
      unless prev == Backports::Undefined
        if block.call(prev, elem)
          y.yield acc
          acc = []
        end
      end
      acc << elem
      prev = elem
    end
    y.yield acc unless acc.empty?
  end
end
sum(accumulator = 0, &block) click to toggle source
# File lib/backports/2.4.0/enumerable/sum.rb, line 2
def sum(accumulator = 0, &block)
  values = block_given? ? map(&block) : self
  values.inject(accumulator, :+)
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() { |elem| ... } 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
to_h(*args) click to toggle source
# File lib/backports/2.1.0/enumerable/to_h.rb, line 6
def to_h(*args)
  h = {}
  each_entry(*args) do |key_value|
    key_value = Backports.coerce_to_ary(key_value)
    if key_value.size != 2
      raise ArgumentError, "element has wrong array length (expected 2, was #{key_value.size})"
    end
    h[ key_value[0] ] = key_value[1]
  end
  h
end
to_h_with_block(*args, &block) click to toggle source
# File lib/backports/2.6.0/enumerable/to_h.rb, line 7
def to_h_with_block(*args, &block)
  return to_h_without_block(*args) unless block
  map(*args, &block).to_h_without_block
end
uniq(&block) click to toggle source
# File lib/backports/2.4.0/enumerable/uniq.rb, line 2
def uniq(&block)
  to_a.uniq(&block)
end