Git Product home page Git Product logo

my_find_code_along-onl01-seng-pt-012120's Introduction

Code Along: Building the find method

##Objectives

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

##Code Along Exercise

Fork and clone this lab. You'll be coding your solution in lib/my_find.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_find 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_find(collection)
  i = 0
  while i < collection.length
    i = i + 1
  end
end

###Step 2 For each element in the collection, you're going to yield to the block. Let's look at the code and break down what's happening.

def my_find(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:

collection = (1..100).to_a
my_find(collection) {|i| i % 3 == 0 and i % 5 == 0 }

As this code executes it will look like this:

def my_find(collection)
  i = 0
  while i < 100
    yield(1)
      i = i + 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_find(collection) {|1| 1 % 3 == 0 and 1 % 5 == 0 }
  #=> false

In this example, ruby will send false (the return value of the block) back to the my_find method because 1 % 3 == 0 and 1 % 5 == 0 evaluates to false.

###Step 3: Using the return value of the yield block The purpose of the find method is to return the first element in the array that evaluates to true not the return value itself. Therefore, let's use the return value to set a conditional. In other words, we want to say something like:

  1. Yield to this block with the given argument
  2. If the block returns true, return the argument we passed into yield AKA the element in the array that evaluated to true. This will exit the loop and the element will be the return value of the method.
  3. If the block returns false, continue to the next element in the array
def my_find(collection)
  i = 0
  while i < collection.length
    if yield(collection[i])
      return collection[i]
    end
    i = i + 1
  end
end

We can refactor this slightly by putting our if statement all on one line using a statement modifier:

def my_find(collection)
  i = 0
  while i < collection.length
    return collection[i] if yield(collection[i])
    i = i + 1
  end
end

Sticking with our previous example:

collection = (1..100).to_a
my_find(collection) {|i| i % 3 == 0 and i % 5 == 0 }

Our method will return the first element in the array that evaluates to true. In our example this would be 15.

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

View My Find Lab on Learn.co and start learning to code for free.

my_find_code_along-onl01-seng-pt-012120's People

Contributors

annjohn avatar gj avatar joshuabamboo avatar kjleitz avatar maxwellbenton avatar morgvanny avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.