Git Product home page Git Product logo

ruby-enumerables-each-the-unexpressive-enumerable-online-web-prework's Introduction

Each: The Unexpressive Enumerable

Learning Goal

  • Use each to Work with an Array
  • Explain why each is the least-expressive Enumerable

Introduction

There are many Enumerable methods that do specific things. In the introduction to this section, we stated that usually an Enumerable performs work on a collection and returns a new collection of results or aggregates the results into a single value. There is one exception to this, and it happens to be the root Enumerable of all other Enumerables - each. In this lesson, we'll talk about each and how we use it.

Use each to Work with an Array

The each method follows the pattern of all Enumerable methods - enumerate over a collection and do some work with each element.

chapter_titles = ["1. The Beginning", "2. The Middle", "3. The End"]
chapter_titles.each do |title|
  puts title
end #=>
# 1. The Beginning
# 2. The Middle
# 3. The End

The each method provides a block where we can access the individual elements in an array. In the example above, title is equal to the current array element. each visits every element and yields to the block, allowing us to do something with the element. Here, we're using puts to print each element out.

Explain Why each is the Least-Expressive Enumerable

We are teaching each early mainly for one reason - it is possible to use each to recreate the behavior of all other Enumerables. For instance, we just learned a bit about count in the previous lesson. We could achieve the behavior of count using each and a variable. The following two code snippets achieve the same result:

[1,2,3,4].count do |num|
  num.even?
end
 # => 2
total = 0
[1,2,3,4].each do |num|
  if num.even?
    total += 1
  end
end
total
 # => 2

While both code snippets are valid, one is more expressive. count communicates more to other programmers about what it is we're trying to do.

If you find yourself using each to collect data like we did above, there is likely a better, more specific Enumerable built for the task. Sure, it works, but it doesn't communicate. We should always strive to have code that communicates first and works second.

Identify Cases for each

The best time to use each is when you need to enumerate a collection but aren't transforming data. It's also a good starting place for when you're not quite sure which Enumerable you want to use. The best use is to print out something to the screen:

oppressed_workers = ["dopey", "sneezy", "happy", "angry", "doc", "lemonjello", "sleepy" ]
oppressed_workers.each do |oppressed_worker|
   puts "#{oppressed_worker.capitalize} wants to start a union!"
end #=>
# Dopey wants to start a union!
# Sneezy wants to start a union!
# Happy wants to start a union!
# Angry wants to start a union!
# Doc wants to start a union!
# Lemonjello wants to start a union!
# Sleepy wants to start a union!

We aren't concerned with changing the original array, nor are we concerned with what is returned in the block. Remember that with count, elements were counted based on whether or not the block returned a truthy value. For each, nothing needs to be returned in the block because each doesn't transform the data.

List each Variants

The each method has a few close cousins that provide slight variations in behavior. You want to recognize them, but you needn't memorize them. Consult the [Enumerables][enumdoc] documentation to see how the following work:

  • each_cons
  • each_entry
  • each_slice
  • each_with_index
  • each_with_object (a cousin of reduce)

Conclusion

This concludes our discussion of each. It's the most flexible Enumerable, but it's also the least expressive. When you aren't sure which way to go, you might want to use it, but most of the time you really want to use map or reduce.

ruby-enumerables-each-the-unexpressive-enumerable-online-web-prework's People

Contributors

maxwellbenton avatar

Watchers

Mohawk Greene avatar Victoria Thevenot avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar  avatar Ben Oren avatar Matt avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Dominique De León 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.