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

Instance Method Details

#apply(method, *args) ⇒ Array

Applies a method to the collection of various arguments.

Examples:

Requires multiple files

# Without Nutella:
%w[several libs to include].each { |r| require r }
# With Nutella:
%w[several libs to include].apply :require

Passes in multiple arguments

# Without Nutella:
[1, 2, 3].each { |n| foo n, 5 }
# With Nutella:
[1, 2, 3].apply :foo, 5

Uses a multi-dimensional array to get the extra arguments

arr = [[1, 2], [3, 4], [5, 6]]
# Without Nutella:
arr.each { |a, b| foo a, b }
# With Nutella:
arr.apply :foo

Parameters:

  • method (Symbol)

    the method to apply

  • args (*Object)

    any extra arguments to use

Returns:

  • (Array)

    a list of the return values of the method calls



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?.

Parameters:

  • object (Object)

    the object to test for exclusion

Returns:

  • (Boolean)

    whether or not the collection excludes object



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.

Examples:

Groups numbers 1..4 into groups of 2

(1..4).group(2)       # => [[1, 2], [3, 4]]

Groups numbers 1..8 into groups of 3

(1..8).group(3)       # => [[1, 2, 3], [4, 5, 6], [7, 8]]

Groups numbers 1..8 into groups of 3, discarding excess elements

(1..8).group(3, true) # => [[1, 2, 3], [4, 5, 6]]

Parameters:

  • size (Integer)

    the size of the groups to create

  • discard_excess (Boolean) (defaults to: false)

    whether or not to discard extra elements

Returns:

  • (Array)

    an array of the groups



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.

See Also:



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.

Examples:

Replaces all vowels with "x" in a list of strings

%w[Ann Mary Paul].map :gsub, /[aeiou]/i, "x" # => ["xnn", "Mxry", "Pxul"]


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_mapObject



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.

Examples:

Sums up the elements of an array

[1, 2, 3].sum         # => 6

Sums up the elements of a 2-dimensional array

[[1, 2], [3, 4]].sum  # => 10

Sums up the numeric elements of a hash

{ a: 4, b: 5 }.sum    # => 9

Ignores the non-numeric element in the array

[2, "string", 9].sum  # => 11

Sums up the even numbers from 1 to 10

(1..10).sum &:even?

Sums up the numbers from 1 to 100 that are multiples of 3

(1..100).sum { |n| n % 3 == 0 }

Returns:

  • (Numeric)

    the sum of all numeric elements in the collection



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