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
-
#average(default = nil) ⇒ Object
Calculates the average of a numeric collection.
-
#drop_last(n) ⇒ Array
Drops the last n elements of an enumerable.
-
#drop_last_while ⇒ Array
Drops the last elements of an enumerable meeting a predicate.
-
#exactly?(n) ⇒ Boolean
Checks if exactly n elements meet a certain predicate.
-
#frequencies ⇒ Hash
Counts the number of occurrence of items in the enumerable.
-
#several? ⇒ Boolean
Checks if two or more elements meet a certain predicate.
-
#sum(initial = nil) ⇒ Object
Sums up elements of a collection by invoking their ‘+` method.
-
#take_last(n) ⇒ Array
Take the last n elements of an enumerable.
-
#take_last_while ⇒ Array
Take the last n elements of an enumerable meeting a certain predicate.
Instance Method Details
#average(default = nil) ⇒ Object
Calculates the average of a numeric collection.
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.
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_while ⇒ Array
Drops the last elements of an enumerable meeting a predicate.
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.
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 |
#frequencies ⇒ Hash
Counts the number of occurrence of items in the enumerable.
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.
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.
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.
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_while ⇒ Array
Take the last n elements of an enumerable meeting a certain predicate.
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 |