Skip to content

learn-co-students/ruby-enumerables-enumerable-family-tree-den-web-071519

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The Enumerable Family Tree

Learning Goals

  • Use map to transform an Array.
  • Use reduce to reduce an Array to a value.
  • Use Ruby documentation to learn more about other variations on map and reduce.
  • Use select and reject to filter an Array.
  • Provide a list of Enumerables to memorize.

Introduction

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.

Ruby Enumerables

Use map to Transform an Array

[10, 20, 30, 40].map{ |num| num * 2 } #=> [20, 40, 60, 80]

Use reduce to Reduce an Array to a Value

[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!

Use Ruby Documentation to Learn More About Other Variations on map and reduce

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.

Select / Reject

[10, 20, 30, 40].select{ |num| num > 25 } #=> [30, 40]
[10, 20, 30, 40].reject{ |num| num > 25 } #=> [10, 20]
  1. Map a collection
  2. Only accumulate the elements that make a truthy expression in the block for select.
  3. Only accumulate the elements that don't make a truthy expression in the block for reject.

Provide a List of Enumerables to Memorize

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 truthy
  • any?: Did anything "tested" by the block returns truthy
  • collect: Does the same thing as map
  • 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 as detect.
  • find_all: Which elements satisfy the block? Does the same thing as select.
  • 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

Conclusion

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!

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •