Ruby Enumerable Functions

EditRocket provides the following information on Enumerable functions in the Ruby source code builder.

all? - Passes each element of the collection to the given block. The method returns true if the block never returns false or nil. If the block is not given, Ruby adds an implicit block of {|obj| obj} (that is all? will return true only if none of the collection members are false or nil.)

any? - Passes each element of the collection to the given block. The method returns true if the block ever returns a value other than false or nil. If the block is not given, Ruby adds an implicit block of {|obj| obj} (that is any? will return true if at least one of the collection members is not false or nil.

collect - Returns a new array with the results of running block once for every element in enum.

detect(ifnone = nil) - Passes each entry in enum to block. Returns the first for which block is not false. If no object matches, calls ifnone and returns its result when it is specified, or returns nil

each_cons each_cons(n) - Iterates the given block for each array of consecutive <n> elements.

each_slice(n) - Iterates the given block for each slice of <n> elements.

each_with_index - Calls block with two arguments, the item and its index, for each item in enum.

entries - Returns an array containing the items in enum.

enum_cons(n) - Returns Enumerable::Enumerator.new(self, :each_cons, n).

enum_slice(n) - Returns Enumerable::Enumerator.new(self, :each_slice, n).

enum_with_index - Returns Enumerable::Enumerator.new(self, :each_with_index).

find(ifnone = nil) - Passes each entry in enum to block. Returns the first for which block is not false. If no object matches, calls ifnone and returns its result when it is specified, or returns nil

find_all - Returns an array containing all elements of enum for which block is not false (see also Enumerable#reject).

grep(pattern) - Returns an array of every element in enum for which Pattern === element. If the optional block is supplied, each matching element is passed to it, and the block's result is stored in the output array.

include?(obj) - Returns true if any member of enum equals obj. Equality is tested using ==.

inject(initial) - Combines the elements of enum by applying the block to an accumulator value (memo) and each element in turn. At each step, memo is set to the value returned by the block. The first form lets you supply an initial value for memo. The second form uses the first element of the collection as a the initial value (and skips that element while iterating).

map - Returns a new array with the results of running block once for every element in enum.

max - Returns the object in enum with the maximum value. The first form assumes all objects implement Comparable; the second uses the block to return a <=> b.

member?(obj) - Returns true if any member of enum equals obj. Equality is tested using ==.

min - Returns the object in enum with the minimum value. The first form assumes all objects implement Comparable; the second uses the block to return a <=> b.

partition - Returns two arrays, the first containing the elements of enum for which the block evaluates to true, the second containing the rest.

reject - Returns an array for all elements of enum for which block is false (see also Enumerable#find_all).

select - Returns an array containing all elements of enum for which block is not false (see also Enumerable#reject).

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

sort_by - Sorts enum using a set of keys generated by mapping the values in enum through the given block.

to_a - Returns an array containing the items in enum.

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

zip(arg, ...) - Converts any arguments to arrays, then merges elements of enum with corresponding elements from each argument. This generates a sequence of enum#size n-element arrays, where n is one more that the count of arguments. If the size of any argument is less than enum#size, nil values are supplied. If a block given, it is invoked for each output array, otherwise an array of arrays is returned.