module Enumerable

Public Instance Methods

average(default = nil) click to toggle source

Calculates the average of a numeric collection.

@param default [Object] an optional default return value if there are no elements.

It's nil by default.

@return The average of the elements or the default value if there are no

elements.

@example

[1, 2, 3].average #=> 2
[1, 2, 3, 4].average #=> 2.5
[].average #=> nil
[].average(0) #=> 0
# File lib/powerpack/enumerable/average.rb, line 15
def average(default = nil)
  coll_size = to_a.size
  coll_size > 0 ? reduce(&:+) / coll_size.to_f : default
end
drop_last(n) click to toggle source

Drops the last n elements of an enumerable.

@param n [Fixnum] the number of elements to drop @return [Array] an array containing the remaining elements

@example

[1, 2, 3].drop_last(1) #=> [1, 2]
[].drop_last(5) #=> []
# File lib/powerpack/enumerable/drop_last.rb, line 11
def drop_last(n)
  fail ArgumentError, 'attempt to drop negative size' if n < 0

  ary = to_a

  return [] if n > ary.size
  ary[0...(ary.size - n)]
end
drop_last_while() { |obj| ... } click to toggle source

Drops the last elements of an enumerable meeting a predicate.

@return [Array] an array containing the remaining elements

@example

[1, 2, 3].drop_last_while(&:odd?) #=> [1, 2]
# File lib/powerpack/enumerable/drop_last_while.rb, line 9
def drop_last_while
  return to_enum(:drop_last_while) unless block_given?

  result = []
  dropping = true
  reverse_each do |obj|
    result.unshift(obj) unless dropping &&= yield(obj)
  end

  result
end
exactly?(n) { |*o| ... } click to toggle source

Checks if exactly n elements meet a certain predicate.

@param n [Fixnum] the number of matches required @return [Boolean] true if we get exactly n matches, false otherwise

@example

[1, 2, 3, 4].exactly?(1) { |n| n > 3 } #=> true
[1, 2, 3, 4].exactly?(2, &:even?) #=> true
[1, 1, 3, 3].exactly?(2, &:even?) #=> false

Without a block uses the identify of the elements as default predicate. This means that nil and false elements will be ignored.

@example

[1, false, nil].exactly?(3) #=> false
[1, false, nil].exactly?(1) #=> true
[false, nil].exactly?(0) #=> true
[1, 2, 3].exactly?(3) #=>true
# File lib/powerpack/enumerable/exactly.rb, line 21
def exactly?(n)
  found_count = 0

  if block_given?
    each do |*o|
      if yield(*o)
        found_count += 1
        return false if found_count > n
      end
    end
  else
    each do |o|
      if o
        found_count += 1
        return false if found_count > n
      end
    end
  end

  n == found_count
end
frequencies() click to toggle source

Counts the number of occurrence of items in the enumerable.

@return [Hash] in the format value => count

@example

[].frequencies # => {}
[1, :symbol, 'string', 3, :symbol, 1].frequencies
  #=> { 1 => 2, :symbol => 2, 'string' => 1, 3 => 1 }
# File lib/powerpack/enumerable/frequencies.rb, line 13
def frequencies
  each_with_object(Hash.new(0)) { |e, a| a[e] += 1 }
end
several?() { |*o| ... } click to toggle source

Checks if two or more elements meet a certain predicate.

@example

[1, 2, 3, 4].several?(&:even?) #=> true
[1, 1, 3, 3].several?(&:even?) #=> false

Without a block uses the identify of the elements as default predicate. This means that nil and false elements will be ignored.

@example

[1, false, nil].several? #=> false
[1, 2, 3].several? #=>true
# File lib/powerpack/enumerable/several.rb, line 15
def several?
  found_count = 0

  if block_given?
    each do |*o|
      if yield(*o)
        found_count += 1
        return true if found_count > 1
      end
    end
  else
    each do |o|
      if o
        found_count += 1
        return true if found_count > 1
      end
    end
  end

  false
end
sum(default = nil) click to toggle source

Sums up elements of a collection by invoking their `+` method. Most useful for summing up numbers.

@param default [Object] an optional default return value if there are no elements.

It's nil by default.

@return The sum of the elements or the default value if there are no

elements.

@example

[1, 2, 3].sum #=> 6
["a", "b", "c"].sum #=> "abc"
[[1], [2], [3]].sum #=> [1, 2, 3]
[].sum #=> nil
[].sum(0) #=> 0
# File lib/powerpack/enumerable/sum.rb, line 17
def sum(default = nil)
  reduce(&:+) || default
end
take_last(n) click to toggle source

Take the last n elements of an enumerable.

@param n [Fixnum] the number of elements to take @return [Array] an array containing the requested elements

@example

[1, 2, 3].take_last(2) #=> [2, 3]
[].take_last(5) #=> []
# File lib/powerpack/enumerable/take_last.rb, line 11
def take_last(n)
  fail ArgumentError, 'attempt to take negative size' if n < 0

  ary = to_a

  return ary if n > ary.size
  ary[(ary.size - n)..-1]
end
take_last_while() { |elem| ... } click to toggle source

Take the last n elements of an enumerable meeting a certain predicate.

@return [Array] an array containing the matching elements

@example

[1, 2, 3, 5].take_last_while(&:odd?) #=> [3, 5]
# File lib/powerpack/enumerable/take_last_while.rb, line 9
def take_last_while
  return to_enum(:take_last_while) unless block_given?

  result = []
  reverse_each { |elem| yield(elem) ? result.unshift(elem) : break  }
  result
end