My Find Lab
##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
):
###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.
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:
As this code executes it will look like this:
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
:
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:
Yield to this block with the given argument
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.If the block returns false, continue to the next element in the array
We can refactor this slightly by putting our if
statement all on one line using a statement modifier:
Sticking with our previous example:
Our method will return the first element in the array that evaluates to true
. In our example this would be 15.
Last updated