You’re probably afraid to ask about some of the methods in the Ruby Enumerable mixin. Not only are there a whole bunch of them, 41 in Ruby 1.9 to be exact, but selecting the right one for the problem can be an exercise in frustration. Part of the problem is there’s no easy way to identify what method might suit your requirements. While the names are usually self-explanatory, it can be tricky to remember block arguments or if they return an Array or a particular element.
Any toolbox needs to be organized, so it’s probably best to sort the various Enumerable methods into groups based on the kind of result you want to get. Grouped together like this, the methodology within Ruby becomes more apparent.
Conventions
In this description, “true / false” refers to values that are equivalent to true, or equivalent to false. In practical terms this means that nil and false are both considered false, and everything else is non-false, (or in general terms, true). Note: things that evaluate as true include what might be considered false in other languages such as 0, empty strings, and empty Array or Hash structures.
When describing the blocks, e refers to an element in the set. For an Array, this is simply the element at a particular index. For a Hash this is a key/value pair, so the block semantics should be expanded to { |(k,v)| } or e.first and e.last will need to be used within the block.
Assessment
One of the simplest features of Enumerable is the methods that provide a quick true/false assessment based on the content involved. Often these will be used to decide how to handle an Array or Hash, or if its in a condition that can be utilized.
| Method |
Arguments |
Block Definition |
Return |
| all? |
|
{ |e| … } => true / false
* Optional |
true / false |
| any? |
|
{ |e| … } => true / false
* Optional |
true / false |
| none? |
|
{ |e| … } => true / false
* Optional |
true / false |
| one? |
|
{ |e| … } => true / false
* Optional |
true / false |
Single Element
Often you’ll want to extract a single element from a set. In this case there are many ways to get what you want, each with their particular quirks.
| Method |
Arguments |
Block Definition |
Return |
| detect |
ifnone = nil |
{ |e| … } => true / false |
element / nil |
| find |
ifnone = nil |
{ |e| … } => true / false |
element / nil |
| first |
|
|
element / nil |
| max |
|
{ |a,b| … } => -1 / 0 / 1
* Optional |
element / nil |
| max_by |
|
{ |e| … } |
element / nil |
| min |
|
{ |a,b| … } => -1 / 0 / 1
* Optional |
element / nil |
| min_by |
|
{ |e| … } |
element / nil |
Filtering
| Method |
Arguments |
Block Definition |
Return |
| grep |
regexp |
{ |e| … }
* Optional |
Array |
| reject |
|
{ |e| … } => true / false |
Array |
| select |
|
{ |e| … } => true / false |
Array |
| select |
|
{ |e| … } => true / false |
Array |
Transformation
There are a few powerful methods for transforming the content of one set into an Array.
| Method |
Arguments |
Block Definition |
Return |
| collect |
|
{ |e| … } |
Array |
| map |
|
{ |e| … } |
Array |
| inject |
memo = first |
{ |memo, e| … } |
last block result / nil |