Module: Enumerable

Defined in:
lib/powerpack/enumerable/sum.rb,
lib/powerpack/enumerable/average.rb,
lib/powerpack/enumerable/exactly.rb,
lib/powerpack/enumerable/several.rb,
lib/powerpack/enumerable/drop_last.rb,
lib/powerpack/enumerable/take_last.rb,
lib/powerpack/enumerable/frequencies.rb,
lib/powerpack/enumerable/drop_last_while.rb,
lib/powerpack/enumerable/take_last_while.rb

Instance Method Summary collapse

Instance Method Details

#average(default = nil) ⇒ Object

Calculates the average of a numeric collection.

Examples:

[1, 2, 3].average #=> 2
[1, 2, 3, 4].average #=> 2.5
[].average #=> nil
[].average(0) #=> 0

Parameters:

  • default (Object) (defaults to: nil)

    an optional default return value if there are no elements. It’s nil by default.

Returns:

  • The average of the elements or the default value if there are no elements.



15
16
17
18
# 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) ⇒ Array

Drops the last n elements of an enumerable.

Examples:

[1, 2, 3].drop_last(1) #=> [1, 2]
[].drop_last(5) #=> []

Parameters:

  • n (Fixnum)

    the number of elements to drop

Returns:

  • (Array)

    an array containing the remaining elements



11
12
13
14
15
16
17
18
# 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_whileArray

Drops the last elements of an enumerable meeting a predicate.

Examples:

[1, 2, 3].drop_last_while(&:odd?) #=> [1, 2]

Returns:

  • (Array)

    an array containing the remaining elements



9
10
11
12
13
14
15
16
17
18
19
# 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) ⇒ Boolean

Checks if exactly n elements meet a certain predicate.

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

Examples:

[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
[1, false, nil].exactly?(3) #=> false
[1, false, nil].exactly?(1) #=> true
[false, nil].exactly?(0) #=> true
[1, 2, 3].exactly?(3) #=>true

Parameters:

  • n (Fixnum)

    the number of matches required

Returns:

  • (Boolean)

    true if we get exactly n matches, false otherwise



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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

#frequenciesHash

Counts the number of occurrence of items in the enumerable.

Examples:

[].frequencies # => {}
[1, :symbol, 'string', 3, :symbol, 1].frequencies
  #=> { 1 => 2, :symbol => 2, 'string' => 1, 3 => 1 }

Returns:

  • (Hash)

    in the format value => count



13
14
15
# File 'lib/powerpack/enumerable/frequencies.rb', line 13

def frequencies
  each_with_object(Hash.new(0)) { |e, a| a[e] += 1 }
end

#several?Boolean

Checks if two or more elements meet a certain predicate.

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

Examples:

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

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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(initial = nil) ⇒ Object

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

Examples:

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

Parameters:

  • initial (Object) (defaults to: nil)

    an optional initial value. It defaults to 0 for an empty collection.

Returns:

  • The sum of the elements, or the initial value if there are no elements.



17
18
19
20
21
22
23
# File 'lib/powerpack/enumerable/sum.rb', line 17

def sum(initial = nil)
  if initial
    reduce(initial, &:+)
  else
    reduce(&:+) || 0
  end
end

#take_last(n) ⇒ Array

Take the last n elements of an enumerable.

Examples:

[1, 2, 3].take_last(2) #=> [2, 3]
[].take_last(5) #=> []

Parameters:

  • n (Fixnum)

    the number of elements to take

Returns:

  • (Array)

    an array containing the requested elements



11
12
13
14
15
16
17
18
# 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_whileArray

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

Examples:

[1, 2, 3, 5].take_last_while(&:odd?) #=> [3, 5]

Returns:

  • (Array)

    an array containing the matching elements



9
10
11
12
13
14
15
# 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