GithubHelp home page GithubHelp logo

ruby-enumerables-enumerable-family-tree-london-web-030920's Introduction

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!

ruby-enumerables-enumerable-family-tree-london-web-030920's People

Contributors

maxwellbenton avatar thuyanduong-flatiron avatar drakeltheryuujin avatar

Watchers

 avatar  avatar Victoria Thevenot avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar  avatar  avatar Ben Oren avatar Matt avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Kaeland Chatman avatar  avatar Lisa Jiang avatar Vicki Aubin avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.