Enumerable??™s methods fall into several categories.
Predicates
These represent properties of a collection that may be true or false.
all?
Returns true if the given block evaluates to true for all items in the collection.
any?
Returns true if the given block evaluates to true for any item in the collection.
include?(x), member?(x)
Returns true if x is a member of the collection.
Filters
These methods return a subset of the items in the collection.
detect, find
Returns the first item in the collection for which the block evaluates to true, or
nil if no such item was found.
select, find_all
Returns an array of all items in the collection for which the block evaluates to
true.
38 | Chapter 1: Foundational Techniques
reject
Returns an array of all items in the collection for which the block evaluates to
false.
grep(x)
Returns an array of all items in the collection for which x=== item is true. This
usage is equivalent to select{|item| x === item}.
Transformers
These methods transform a collection into another collection by one of several rules.
map, collect
Returns an array consisting of the result of the given block being applied to each
element in turn.
partition
Equivalent to [select(&block), reject(&block)].
sort
Returns a new array of the elements in this collection, sorted by either the given
block (treated as the <=> method) or the elements??™ own <=> method.
Pages:
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71