new

Lazy.new(obj, size=nil) { |yielder, *values| ... } Class Public methods Creates a new Lazy enumerator. When the enumerator is actually enumerated (e.g. by calling force), obj will be enumerated and each value passed to the given block. The block can yield values back using yielder. For example, to create a method filter_map in both lazy and non-lazy fashions: module Enumerable def filter_map(&block) map(&block).compact end end class Enumerator::Lazy def filter_m

zip

enum.zip(arg, ...) â an_array_of_arrayenum.zip(arg, ...) { |arr| block } â nil Instance Public methods Takes one element from enum and merges corresponding elements from each args. This generates a sequence of n-element arrays, where n is one more than the count of arguments. The length of the resulting sequence will be enum#size. If the size of any argument is less than enum#size, nil values are supplied. If a block is given, it is invoked for each output arr

to_set

to_set(klass = Set, *args, &block) Instance Public methods Makes a set from the enumerable object with given arguments. Needs to +require âsetâ+ to use this method.

to_a

enum.to_a â array Instance Public methods Returns an array containing the items in enum. (1..7).to_a #=> [1, 2, 3, 4, 5, 6, 7] { 'a'=>1, 'b'=>2, 'c'=>3 }.to_a #=> [["a", 1], ["b", 2], ["c", 3]]

take_while

enum.take_while { |arr| block } â arrayenum.take_while â an_enumerator Instance Public methods Passes elements to the block until the block returns nil or false, then stops iterating and returns an array of all prior elements. If no block is given, an enumerator is returned instead. a = [1, 2, 3, 4, 5, 0] a.take_while { |i| i < 3 } #=> [1, 2]

take

enum.take(n) â array Instance Public methods Returns first n elements from enum. a = [1, 2, 3, 4, 5, 0] a.take(3) #=> [1, 2, 3]

sort_by

enum.sort_by { |obj| block } â arrayenum.sort_by â an_enumerator Instance Public methods Sorts enum using a set of keys generated by mapping the values in enum through the given block. If no block is given, an enumerator is returned instead. %w{apple pear fig}.sort_by { |word| word.length} #=> ["fig", "pear", "apple"] The current implementation of sort_by generates an array of tuples containing the original collection element and the mapped va

sort

enum.sort â arrayenum.sort { |a, b| block } â array Instance Public methods Returns an array containing the items in enum sorted, either according to their own <=> method, or by using the results of the supplied block. The block should return -1, 0, or +1 depending on the comparison between a and b. As of Ruby 1.8, the method Enumerable#sort_by implements a built-in Schwartzian Transform, useful when key computation or comparison is expensive. %w(rhea kea fl

slice_before

enum.slice_before(pattern) â an_enumeratorenum.slice_before { |elt| bool } â an_enumeratorenum.slice_before(initial_state) { |elt, state| bool } â an_enumerator Instance Public methods Creates an enumerator for each chunked elements. The beginnings of chunks are defined by pattern and the block. If pattern === elt returns true or the block returns true for the element, the element is beginning of a chunk. The === and block is calle

select

enum.select { |obj| block } â arrayenum.select â an_enumerator Instance Public methods Returns an array containing all elements of enum for which the given block returns a true value. If no block is given, an Enumerator is returned instead. (1..10).find_all { |i| i % 3 == 0 } #=> [3, 6, 9] [1,2,3,4,5].select { |num| num.even? } #=> [2, 4] See also #reject.