languages | tags | resources |
---|---|---|
ruby |
todo, blocks, tdd, metaprogramming, enumerable, iteration |
4 |
Read about the yield keyword and ruby blocks. It's all about differing 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.
Think about what's going on in "each". It's looping through the elements of an array and yielding you the individual elements one at a time to the block. What has to happen to do this?
Note: All Ruby methods accept blocks by default.
Write your code in "my_each.rb".