Git Product home page Git Product logo

my-each-001-prework-web's Introduction

My Each

Objectives

  1. Understand how the yield keyword works in Ruby.
  2. Practice using yield with blocks.
  3. Gain a deeper understanding of the common iterator .each.

The yield Keyword

The yield keyword, when used inside a method, will allow you to call that method with a block and pass, or "yield", to that block. Think of the yield keyword as saying "stop executing the code in this method and instead execute the code in the block. Then, return to the code in the method."

Let's look at the following example:

def yielding
  puts "the program is executing the code inside the method"
  yield
  puts "now we are back in the method"
end

yielding {puts "the method has yielded to the block!"}

When we call yielding with the above block, we will output:

"the program is executing the code inside the method"
"the method has yielded to the block!"
"now we are back in the method"

Yielding With Parameters

The yield keyword can take parameters. In other words, if you use yield and give it an argument, it will pass that argument to the block and that data will become available to the code in the block.

For example:

def yielding_with_arguments(num)
  puts "the program is executing the code inside the method"
  i = num
  yield(i)
  puts "now we are back in the method"
end

yielding_with_arguments(2) {|i| puts i * 2}

Will output:

"the program is executing the code inside the method"
4
"now we are back in the method"

The syntax inside the block might look familiar—it is how we identify index items in a block when we call .each on an array and pass a block to that method call.

Enumerators Under the Hood

You've already worked with enumerator methods like .each, .collect and others. These methods are called on collections, like arrays. They take blocks as their arguments and yield each element of the collection to the block, allowing the code in the block to be applied to each element of the collection.

You can read more about the yield keyword and blocks in Ruby from the resources below. It's all about delegating the execution to an abstract procedure or block.

Instructions

Now that you know how the yield method works, try to write your own version of the .each method without using the .each method provided by Ruby. As in, try to build my_each using only the while keyword and the yield.

Fork and clone this repository. Run the test suite with the learn command to gain a better understanding of what is being asked of you. You'll be writing your code in my_each.rb

Think about what's going on in .each. It's looping through the elements of an array and yielding the individual elements one at a time to the block. What has to happen to do this?

Here's an example of what should happen when you call your my_each method:

collection = [1, 2, 3, 4]
my_each(collection) do |i|
  puts i
end

This should output:

1
2
3
4

and return:

#=> [1, 2, 3, 4]

Just like the real .each method.

Note: All Ruby methods accept blocks by default.

Resources

my-each-001-prework-web's People

Contributors

sarogers avatar kthffmn avatar aviflombaum avatar deniznida avatar octosteve avatar ahimmelstoss avatar sophiedebenedetto avatar arelenglish avatar fs-lms-test-bot avatar irmiller22 avatar loganhasson avatar fislabstest avatar markedwardmurray avatar mbukantz avatar

Watchers

James Cloos 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.