GithubHelp home page GithubHelp logo

ruby-yield-practice-my-all-staff's Introduction

Code-Along: Building the all? method

Objectives

  • Continue to practice using yield and blocks
  • Gain a deeper understanding of #all?

Code Along Exercise

Fork and clone this lab. You'll be coding your solution in lib/my_all.rb. If at any point you want to follow along with the example, you can put a binding.pry anywhere in the method and run ruby bin/my_all in your terminal to pry around.

Step 1

We need to iterate through each element in the passed in collection using the simple lower-level iterator while. Remember, since this is a lower level iterator, we will need to explicitly add a counter we'll call i, and increment it each time we go into the loop (i = i + 1):

def my_all?(collection)
  i = 0
  while i < collection.length
    i = i + 1   #i += 1 does the same thing. Use this if it's easier for you.
  end
end

Step 2

We're going to yield each element in the collection to the block. Let's look at the code and break down what's happening.

def my_all?(collection)
  i = 0
  while i < collection.length
    yield(collection[i])
    i = i + 1
  end
end

When this code is run and it hits the yield line, it is going to send whatever is passed in as the argument to the block.

Note: If you are confused about where the block comes from, it becomes clearer after the method is called. Here's an example:

my_all?([1,2,3]) {|i| i < 2}

As this code executes it will look like this:

def my_all?([1,2,3])
  i = 0
  while i < 3
    yield(1)
    i = 0 + 1
  end
end

When it hits the yield(1), it is going to send 1 to the block, evaluate it, and send the return value back to yield:

my_all?([1,2,3]) {|1| 1 < 2}
  #=> true

In this example, ruby will send true (the return value of the block) back to the my_all? method because 1 < 2 evaluates to true.

Step 3: Save the return value of the yield block

Since we are looping through several elements in an array, what data structure can we use to store multiple values? An array! First, we'll declare our array before entering the while loop: block_return_values = []. Then, in the loop, let's shovel (<<) the return value of the block into the array: block_return_values << yield(collection[i]).

def my_all?(collection)
  i = 0
  block_return_values = []
  while i < collection.length
    block_return_values << yield(collection[i])
    i = i + 1
  end
end

Step 4: Determine the return value of the method

The return value of all? is simply true or false. If any element in the collection evaluates to false, then all? should return false. If they are all true, the method should return true.

Right now, we have access to an array of return values block_return_values. All we have to do now is determine whether the array contains any false elements.

Sticking with our previous example:

my_all?([1,2,3]) {|i| i < 2}

Our block_return_values would look like this:

block_return_values = [true, false, false]

Let's just add an #include? method to determine the return value of the my_all? method. After we have the return value, we are all set. The final product will look like this:

def my_all?(collection)
  i = 0
  block_return_values = []
  while i < collection.length
    block_return_values << yield(collection[i])
    i = i + 1
  end
  
  if block_return_values.include?(false)
    false
  else
    true
  end
end

View Code Along: Building the all? method on Learn.co and start learning to code for free.

ruby-yield-practice-my-all-staff's People

Contributors

joshuabamboo avatar maxwellbenton avatar pletcher avatar sandyvern avatar ga-be avatar

Watchers

 avatar Mohawk Greene avatar Victoria Thevenot avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar  avatar  avatar Matt avatar Antoin avatar  avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Kaeland Chatman 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.