- Understand how the
yield
keyword works in Ruby. - Practice using
yield
with blocks. - Gain a deeper understanding of the common iterator
.each
.
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"
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.
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.
- All About Ruby - Ruby Blocks
- About.com - Block Parameters and Yielding
- Coda Hale's Blog - Writing a Method that Uses Code Blocks
- Reactive's Tips - Understanding Ruby Blocks, Procs, and Lambdas
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.