Contents
OVERVIEW
EACH_WITH_INDEX
EACH METHOD
TRANSFORMING ARRAYS WITH MAP
THE SELECT AND INJECT METHODS
Overview
Overview
The Enumerable module provides a set of
methods to traverse, search, sort and
manipulate collections.
With Enumerable, you can write prettier,
fewer lines of code.
each method
each method
Loop using for
each method
Convert to each
each_with_index
each_with_in
dex
The Enumerable module has the convenient
each_with_index, which has an extra argument to
refer to the index:
Transforming arrays with
map
Transforming
arrays with
map
Create a transformed copy of array without
altering the original
Transforming
arrays with
map
modify an array, you can use map!
The select and inject
methods
select
This useful method takes in one argument. The block you pass it should be some
kind of true/false test. If the expression results in true for an element in an array,
that element is kept as part of the returned collection
inject
The inject method takes a collection and reduces it to a single value, such as a
sum of values: