Module: Enumerable

Defined in:
lib/lookaround-enumerable.rb

Aliases collapse

Instance Method Summary collapse

Instance Method Details

#collect_with_prev(*args) {|(*args), (*previous)| ... } ⇒ Object #collect_with_prev(*args) ⇒ Enumerator

Aliases for each_with_prev(*args).map

Overloads:

  • #collect_with_prev(*args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_prev

    Returns:

    • (Object)

      The result of the map

  • #collect_with_prev(*args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 354


#collect_with_window(*args) {|(*args), (*previous)| ... } ⇒ Object #collect_with_window(*args) ⇒ Enumerator

Aliases for each_with_window(*args).map

Overloads:

  • #collect_with_window(*args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_window

    Returns:

    • (Object)

      The result of the map

  • #collect_with_window(*args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 550


#each_with_prev(size = 1, crop: false, trim: false, filler: nil, expand: :single) {|(*args), (*previous)| ... } ⇒ Enumerable #each_with_prev(size = 1, crop: false, trim: false, filler: nil, expand: :single) ⇒ Enumerator

Calls block with two (or more, depending on the expand) arguments: the item and the values at earlier indexes.

If no block is given, an enumerator is returned instead.

expand:

(Symbol) Valid options are:

:none

Never expands, block always takes two arguments, each an array

(1..3).each_with_prev(expand: :none) {|(current), (prev)|  }
(1..3).each_with_object({}).each_with_prev(2, expand: :none) {|(current, obj), (prev, prev2)| }
:single

(default) Expands if only one element, block always takes two arguments, each an array or an object

(1..3).each_with_prev(expand: :single) {|current, prev|  }
(1..3).each_with_prev(2, expand: :single) {|current, (prev, prev2)| }
(1..3).each_with_object({}).each_with_prev(2, expand: :single) {|(current, obj), (prev, prev2)| }
:all

Expands all sides, block takes two or more arguments, each an object

(1..3).each_with_prev(expand: :all) {|current, prev|  }
(1..3).each_with_prev(2, expand: :all) {|current, prev, prev2| }
(1..3).each_with_object({}).each_with_prev(2, expand: :all) {|current, obj, prev, prev2| }

Examples:

Size Examples

(1..3).each_with_prev(1).to_a # => [[1, nil], [2, 1], [3, 2]]
(1..3).each_with_prev(2).to_a # => [[1, [nil, nil]], [2, [1, nil]], [3, [2, 1]]]

Crop Examples

(1..3).each_with_prev(1, crop: false).to_a # => [[1, nil], [2, 1], [3, 2]]
(1..3).each_with_prev(1, crop: true).to_a # =>            [[2, 1], [3, 2]]

Trim Examples

(1..3).each_with_object({}).each_with_prev(1, trim: false, expand: :all).to_a
# => [[1, {}, nil], [2, {}, [1, {}]], [3, {}, [2, {}]]]
(1..3).each_with_object({}).each_with_prev(1, trim: true, expand: :all).to_a
# => [[1, {}, nil], [2, {}, 1], [3, {}, 2]]

Filler Examples

(1..3).each_with_prev(1, filler: nil).to_a # => [[1, nil], [2, 1], [3, 2]]
(1..3).each_with_prev(1, filler: 0).to_a   # => [[1, 0], [2, 1], [3, 2]]

Overloads:

  • #each_with_prev(size = 1, crop: false, trim: false, filler: nil, expand: :single) {|(*args), (*previous)| ... } ⇒ Enumerable

    Returns The parent Enumerable.

    Yields:

    • ((*args), (*previous))

    Returns:

  • #each_with_prev(size = 1, crop: false, trim: false, filler: nil, expand: :single) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

Parameters:

  • size (Integer)

    The number of previous elements to iterate with

  • crop: (Bool)

    true if the iteration should only include values with no empty previous (skips first size number of elements), false if all elements iterated

  • trim: (Bool)

    true if the previous elements should only be the first item from lower level iterators, false if all values should be saved. Note that setting this value is only useful on Enumerators or Enumerables that have more than one value.

  • filler: (Any)

    the value to use for empty/no-value history cells at the start of the iteration. Does nothing when crop is true.

  • expand: (Symbol)

    What argument style the block expects. See the valid options above (html doc) or below (source)

Raises:

  • (ArgumentError)

    If the arguments are invalid



# File 'lib/lookaround-enumerable.rb', line 229


#each_with_window(view = -1..1, crop: false, trim: false, filler: nil, expand: :single) {|(*left), (*args), (*right)| ... } ⇒ Enumerable #each_with_window(size = -1..1, crop: false, trim: false, filler: nil, expand: :single) ⇒ Enumerator

Calls block with three (or more, depending on the expand) arguments: the item and the values at earlier and later indexes.

If no block is given, an enumerator is returned instead.

Important note: each_with_window is executed in a disjunct manner from the parent iterator. This can cause issues when chained with other Enumerators. Please use the built in helpers.

Examples:

View Examples

('a'..'d').each_with_window(-2..1).to_a # =>  [[[nil, nil], "a", ["b"]], [[nil, "a"], "b", ["c"]], [["a", "b"], "c", ["d"]], [["b", "c"], "d", [nil]]]

Overloads:

  • #each_with_window(view = -1..1, crop: false, trim: false, filler: nil, expand: :single) {|(*left), (*args), (*right)| ... } ⇒ Enumerable

    Returns The parent Enumerable.

    Yields:

    • ((*left), (*args), (*right))

    Returns:

  • #each_with_window(size = -1..1, crop: false, trim: false, filler: nil, expand: :single) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

Parameters:

  • view (Range)

    The number of items to look at to previous and upcoming indicies.

Raises:

  • (ArgumentError)

    If the arguments are invalid

See Also:



# File 'lib/lookaround-enumerable.rb', line 303


#find_all_with_prev(*args) {|(*args), (*previous)| ... } ⇒ Object #find_all_with_prev(*args) ⇒ Enumerator

Aliases for select.each_with_prev(*args)

Overloads:

  • #find_all_with_prev(*args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_prev

    Returns:

    • (Object)

      The result of the map

  • #find_all_with_prev(*args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 420


#find_all_with_window(*args) {|(*args), (*previous)| ... } ⇒ Object #find_all_with_window(*args) ⇒ Enumerator

Aliases for select.each_with_window(*args)

Overloads:

  • #find_all_with_window(*args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_window

    Returns:

    • (Object)

      The result of the map

  • #find_all_with_window(*args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 616


#inject_with_prev(memo = first, *args) {|(*args), (*previous)| ... } ⇒ Object #inject_with_prev(memo = first, *args) ⇒ Enumerator

Aliases for inject(memo).each_with_prev(*args)

Overloads:

  • #inject_with_prev(memo = first, *args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_prev

    Returns:

    • (Object)

      The result of the map

  • #inject_with_prev(memo = first, *args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 469


#inject_with_window(memo = first, *args) {|(*args), (*previous)| ... } ⇒ Object #inject_with_window(memo = first, *args) ⇒ Enumerator

Aliases for inject(memo).each_with_window(*args)

Overloads:

  • #inject_with_window(memo = first, *args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_window

    Returns:

    • (Object)

      The result of the map

  • #inject_with_window(memo = first, *args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 665


#map_with_prev(*args) {|(*args), (*previous)| ... } ⇒ Object #map_with_prev(*args) ⇒ Enumerator

Aliases for each_with_prev(*args).map

Overloads:

  • #map_with_prev(*args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_prev

    Returns:

    • (Object)

      The result of the map

  • #map_with_prev(*args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 338


#map_with_window(*args) {|(*args), (*previous)| ... } ⇒ Object #map_with_window(*args) ⇒ Enumerator

Aliases for each_with_window(*args).map

Overloads:

  • #map_with_window(*args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_window

    Returns:

    • (Object)

      The result of the map

  • #map_with_window(*args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 534


#pcollect(*args) {|(*args), (*previous)| ... } ⇒ Object #pcollect(*args) ⇒ Enumerator

Aliases for each_with_prev(*args).map

Overloads:

  • #pcollect(*args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_prev

    Returns:

    • (Object)

      The result of the map

  • #pcollect(*args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 386


#pfind_all(*args) {|(*args), (*previous)| ... } ⇒ Object #pfind_all(*args) ⇒ Enumerator

Aliases for select.each_with_prev(*args)

Overloads:

  • #pfind_all(*args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_prev

    Returns:

    • (Object)

      The result of the map

  • #pfind_all(*args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 452


#pinject(memo = first, *args) {|(*args), (*previous)| ... } ⇒ Object #pinject(memo = first, *args) ⇒ Enumerator

Aliases for inject(memo).each_with_prev(*args)

Overloads:

  • #pinject(memo = first, *args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_prev

    Returns:

    • (Object)

      The result of the map

  • #pinject(memo = first, *args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 501


#pmap(*args) {|(*args), (*previous)| ... } ⇒ Object #pmap(*args) ⇒ Enumerator

Aliases for each_with_prev(*args).map

Overloads:

  • #pmap(*args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_prev

    Returns:

    • (Object)

      The result of the map

  • #pmap(*args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 370


#preduce(memo = first, *args) {|(*args), (*previous)| ... } ⇒ Object #preduce(memo = first, *args) ⇒ Enumerator

Aliases for inject(memo).each_with_prev(*args)

Overloads:

  • #preduce(memo = first, *args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_prev

    Returns:

    • (Object)

      The result of the map

  • #preduce(memo = first, *args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 517


#pselect(*args) {|(*args), (*previous)| ... } ⇒ Object #pselect(*args) ⇒ Enumerator

Aliases for select.each_with_prev(*args)

Overloads:

  • #pselect(*args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_prev

    Returns:

    • (Object)

      The result of the map

  • #pselect(*args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 436


#reduce_with_prev(memo = first, *args) {|(*args), (*previous)| ... } ⇒ Object #reduce_with_prev(memo = first, *args) ⇒ Enumerator

Aliases for inject(memo).each_with_prev(*args)

Overloads:

  • #reduce_with_prev(memo = first, *args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_prev

    Returns:

    • (Object)

      The result of the map

  • #reduce_with_prev(memo = first, *args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 485


#reduce_with_window(memo = first, *args) {|(*args), (*previous)| ... } ⇒ Object #reduce_with_window(memo = first, *args) ⇒ Enumerator

Aliases for inject(memo).each_with_window(*args)

Overloads:

  • #reduce_with_window(memo = first, *args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_window

    Returns:

    • (Object)

      The result of the map

  • #reduce_with_window(memo = first, *args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 681


#select_with_prev(*args) {|(*args), (*previous)| ... } ⇒ Object #select_with_prev(*args) ⇒ Enumerator

Aliases for select.each_with_prev(*args)

Overloads:

  • #select_with_prev(*args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_prev

    Returns:

    • (Object)

      The result of the map

  • #select_with_prev(*args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 404


#select_with_window(*args) {|(*args), (*previous)| ... } ⇒ Object #select_with_window(*args) ⇒ Enumerator

Aliases for select.each_with_window(*args)

Overloads:

  • #select_with_window(*args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_window

    Returns:

    • (Object)

      The result of the map

  • #select_with_window(*args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 600


#wcollect(*args) {|(*args), (*previous)| ... } ⇒ Object #wcollect(*args) ⇒ Enumerator

Aliases for each_with_window(*args).map

Overloads:

  • #wcollect(*args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_window

    Returns:

    • (Object)

      The result of the map

  • #wcollect(*args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 582


#wfind_all(*args) {|(*args), (*previous)| ... } ⇒ Object #wfind_all(*args) ⇒ Enumerator

Aliases for select.each_with_window(*args)

Overloads:

  • #wfind_all(*args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_window

    Returns:

    • (Object)

      The result of the map

  • #wfind_all(*args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 648


#winject(memo = first, *args) {|(*args), (*previous)| ... } ⇒ Object #winject(memo = first, *args) ⇒ Enumerator

Aliases for inject(memo).each_with_window(*args)

Overloads:

  • #winject(memo = first, *args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_window

    Returns:

    • (Object)

      The result of the map

  • #winject(memo = first, *args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 697


#wmap(*args) {|(*args), (*previous)| ... } ⇒ Object #wmap(*args) ⇒ Enumerator

Aliases for each_with_window(*args).map

Overloads:

  • #wmap(*args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_window

    Returns:

    • (Object)

      The result of the map

  • #wmap(*args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 566


#wreduce(memo = first, *args) {|(*args), (*previous)| ... } ⇒ Object #wreduce(memo = first, *args) ⇒ Enumerator

Aliases for inject(memo).each_with_window(*args)

Overloads:

  • #wreduce(memo = first, *args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_window

    Returns:

    • (Object)

      The result of the map

  • #wreduce(memo = first, *args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 713


#wselect(*args) {|(*args), (*previous)| ... } ⇒ Object #wselect(*args) ⇒ Enumerator

Aliases for select.each_with_window(*args)

Overloads:

  • #wselect(*args) {|(*args), (*previous)| ... } ⇒ Object

    Returns The result of the map.

    Yields:

    • ((*args), (*previous))

      See the ‘expand` parameter of each_with_window

    Returns:

    • (Object)

      The result of the map

  • #wselect(*args) ⇒ Enumerator

    Returns The external Enumerator.

    Returns:

    • (Enumerator)

      The external Enumerator

See Also:



# File 'lib/lookaround-enumerable.rb', line 632