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/take_last.rb,
lib/powerpack/enumerable/drop_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

- (Object) average(default = nil)

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


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

- (Array) drop_last(n)

Drops the last n elements of an enumerable.

Examples:

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


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

- (Object) drop_last_while



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

- (Boolean) exactly?(n)

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


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

- (Hash) frequencies

Counts the number of ocurrence of items in the enumberable.

Examples:

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


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

- (Boolean) several?

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


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

- (Object) sum(default = nil)

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

Examples:

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


17
18
19
# File 'lib/powerpack/enumerable/sum.rb', line 17

def sum(default = nil)
  reduce(&:+) || default
end

- (Array) take_last(n)

Take the last n elements of an enumerable.

Examples:

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


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

- (Object) take_last_while



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