Book Of Ruby, Chapter 10: Blocks, Procs and Lambdas
With the addition of Chapter 10, the Book Of Ruby now has 211 pages and comes complete with 168 small demo programs to illustrate every topic described in the text.
Well, we’ve finally hit the half-way mark! The Book Of Ruby will eventually have 20 chapters and today I’ve added the tenth of these, which means only another ten to go.... ;-)
This latest chapter deals with a powerful but, for many newcomers to Ruby, potentially highly confusing element of the language: Blocks. These are free-standing bits of code like ‘nameless methods’ or ‘lambda functions’. They can be passed around from one method to another or from one object to another and then executed either by ‘calling’ or ‘yielding’ them. Blocks are not quite objects but can be ‘turned into’ objects called Procs. They also act as ‘closures’ which means that they are able to carry the values of local variables from one place (the block’s original scope) to some other place.
If this all sounds confusing, don’t panic! Blocks, procs and lambdas can, at the outset, be extremely confusing - this latest chapter hopes to cut through that confusion and help you see how blocks can be used in your own applications. Remember that there are lots and lots of sample programs with The Book Of Ruby (18 in this chapter alone!) and it is often easier to understand what is going on by running the programs and then reading the text for further explanation.
More chapters soon...
The Book Of Ruby is a free eBook available on the Download Page.