Module

Enumerable

The Enumerable mixin provides collection classes with several traversal and searching methods, and with the ability to sort. The class must provide a method each, which yields successive members of the collection. If Enumerable#max, #min, or #sort is used, the objects in the collection must also implement a meaningful <=> operator, as these methods rely on an ordering between members of the collection.

Public Methods
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 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_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.
find 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 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? Returns true if any member of enum equals obj. Equality is tested using ==.
inject Combines the elements of enum by applying the block to an
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? 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
to_a Returns an array containing the items in enum.
zip 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.
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.