Lets start off by taking a look at traditional block passing in ruby.
1
| |
Looks great right? Go ahead and pass that block off to another method.
1 2 3 4 5 6 7 8 9 10 | |
Instead of just passing the original block we are forced to create another block which yields to the original block. Of course, we could make the situation a little better by catching the block by reference.
1 2 3 4 | |
This is certainly more DRY and visually pleasing over the first example. Do notice however that Ruby is actually converting our block into a lambda (a Proc object technically) for us now. Even worse is that Ruby handles argument and block syntax somewhat antagonistically.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
A first class lambda would gain us the ability to pass it around, manipulate it, use it as a function / method, and treat it like any other object. Unfortunately this is not entirely the case with Ruby. We have an odd mixture of blocks, lambdas, by reference conversions, and so forth. Having said that, lets see how close we can get to a first class lambda.
I implore you my dear world: let us no longer fail by leaving behind our
daughters.