Module: Enumerable
- Defined in:
- lib/nutella/core_ext/enumerable/map.rb,
lib/nutella/core_ext/enumerable/sum.rb,
lib/nutella/core_ext/enumerable/apply.rb,
lib/nutella/core_ext/enumerable/group.rb,
lib/nutella/core_ext/enumerable/search.rb
Instance Method Summary collapse
-
#apply(method, *args) ⇒ Array
Applies a method to the collection of various arguments.
-
#exclude?(object) ⇒ Boolean
(also: #excludes?)
The inverse of
Enumerable#include?
. -
#group(size, discard_excess = false) ⇒ Array
Groups an array into smaller arrays of
size
elements. -
#group!(size, discard_excess = false) ⇒ Object
Modifies the collection in place as described for
Enumerable#group
. -
#map(*args, &block) ⇒ Object
Allows mapping of a method with parameters across a collection.
- #old_map ⇒ Object
-
#sum(&block) ⇒ Numeric
Add together all numeric elements in the collection.
Instance Method Details
#apply(method, *args) ⇒ Array
Applies a method to the collection of various arguments.
26 27 28 |
# File 'lib/nutella/core_ext/enumerable/apply.rb', line 26 def apply(method, *args) map { |elem| method(method).(*elem, *args) } end |
#exclude?(object) ⇒ Boolean Also known as: excludes?
The inverse of Enumerable#include?
.
9 10 11 |
# File 'lib/nutella/core_ext/enumerable/search.rb', line 9 def exclude?(object) !include? object end |
#group(size, discard_excess = false) ⇒ Array
Groups an array into smaller arrays of size
elements. Remaining elements at the end will be put into a smaller group if necessary, unless discard_excess
is true, in which case they will be discarded.
16 17 18 19 |
# File 'lib/nutella/core_ext/enumerable/group.rb', line 16 def group(size, discard_excess = false) groups = each_slice(size).to_a groups[0..(discard_excess && groups.last.size < size ? -2 : -1)] end |
#group!(size, discard_excess = false) ⇒ Object
Modifies the collection in place as described for Enumerable#group
. Returns the modified collection, or nil if no modifications were made.
26 27 28 29 |
# File 'lib/nutella/core_ext/enumerable/group.rb', line 26 def group!(size, discard_excess = false) return nil if empty? self.replace group(size, discard_excess) end |
#map(*args, &block) ⇒ Object
Allows mapping of a method with parameters across a collection.
You no longer have to open up a block just because the method that you’re mapping needs parameters. This method is still behaves just as it used to otherwise.
12 13 14 |
# File 'lib/nutella/core_ext/enumerable/map.rb', line 12 def map(*args, &block) block_given? ? old_map(&block) : old_map { |elem| elem.send *args } end |
#old_map ⇒ Object
2 |
# File 'lib/nutella/core_ext/enumerable/map.rb', line 2 alias old_map map |
#sum(&block) ⇒ Numeric
Add together all numeric elements in the collection. When using a block, adds together all numeric elements that pass the predicate in that block.
19 20 21 22 23 |
# File 'lib/nutella/core_ext/enumerable/sum.rb', line 19 def sum(&block) # TODO: Two selects in a row, how to DRY this up? flat = respond_to?(:flatten) ? flatten : self flat.select { |e| e.is_a? Numeric }.select(&block).inject(:+) || 0 end |