- Use
map
to transform anArray
. - Use
reduce
to reduce anArray
to a value. - Use Ruby documentation to learn more about other variations on
map
andreduce
. - Use
select
andreject
to filter anArray
. - Provide a list of Enumerables to memorize.
In addition to each
, Ruby has a variety of Enumerables we can use for different
needs. Like each
and count
, these are already built into its Array
data type.
In this lesson, we'll take a look at a few of them - map
, reduce
, select
, and
reject
.
[10, 20, 30, 40].map{ |num| num * 2 } #=> [20, 40, 60, 80]
[10, 20, 30, 40].reduce(0){ |total, num| total + num } #=> 100
[10, 20, 30, 40].reduce(100){ |total, num| total + num } #=> 200
While it was work for us to learn to code these and deal with blocks, using these features is just a few keystrokes!
Now that we understand the "Character of Enumerable Methods" and the code
behind using blocks, you are ready to unleash the full power of this module!
Instead of merely repeating the documentation, we're going to help you apply
your understanding to the select
and reject
methods, and then we're going
to give you a list of document resources you should look up and master.
[10, 20, 30, 40].select{ |num| num > 25 } #=> [30, 40]
[10, 20, 30, 40].reject{ |num| num > 25 } #=> [10, 20]
- Map a collection
- Only accumulate the elements that make a truthy expression in the block for
select
. - Only accumulate the elements that don't make a truthy expression in the
block for
reject
.
These are the Enumerables you should memorize and practice heavily. They're going to be your friends every day in Ruby-land. There are other Enumerables that you might not memorize, but it's pretty common for a developer to realize that they're working in the "Character of Enumerable Methods" and think, "Hmm, maybe there's an Enumerable for that...." When that moment hits, the developer comes back to the documentation and look for that "just-right" Enumerable.
all?
: Everything "tested" by the block returns truthyany?
: Did anything "tested" by the block returns truthycollect
: Does the same thing asmap
count
: Which elements satisfy the block or, without block, how many elements are there?find
: Which element satisfies the block first. Does the same thing asdetect
.find_all
: Which elements satisfy the block? Does the same thing asselect
.find_index
: What is the index of the first element to satisfy the block?max
: What's the highest value?max_by
: What's the highest value based on some property of the element?min
: What's the lowest value?sort
: Put the values in order
Your programming power just increased by at least 10-fold. With these methods, and others provided in the documentation, you can tear through datasets efficiently and flexibly. Enjoy the power!
These Enumerables will help you solve most questions about collections. Some of the Enumerables in the documentation might look a bit scary or make you wonder "When the heck would I ever use that?" But over time, as you become more practiced, you'll be amazed to see these methods rush to your aid!