Module Enumerable
In: lib/powerpack/enumerable/several.rb
lib/powerpack/enumerable/drop_last_while.rb
lib/powerpack/enumerable/drop_last.rb
lib/powerpack/enumerable/average.rb
lib/powerpack/enumerable/take_last.rb
lib/powerpack/enumerable/sum.rb
lib/powerpack/enumerable/exactly.rb
lib/powerpack/enumerable/frequencies.rb
lib/powerpack/enumerable/take_last_while.rb

Methods

Public Instance methods

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

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) #=> []

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]

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

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 }

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

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

@param initial [Object] an optional initial value.

  It defaults to 0 for an empty collection.

@return The sum of the elements, or the initial value if there

  are no elements.

@example

  [1, 2, 3].sum #=> 6
  [[1], [2], [3]].sum #=> [1, 2, 3]
  [].sum #=> 0
  ["a"].sum #=> "a"
  ["b", "c"].sum("a") #=> "abc"

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) #=> []

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]

[Validate]