Book Description
Unannounced and unmarked, blocks turn up all over Ruby programs. Their use ranges from some of Ruby's most basic structures, such as loops, to some of its most sophisticated tricks. Similar to closures or lambdas in other languages, blocks enable some of Ruby's vaunted efficiency. Blocks even allow other people to write some of the code you use.
With this excerpt from Head First Ruby, you'll learn about blocks by looking at each concept from different angles. Exercises throughout will help you understand and remember how these closure-like structures work. In fact, these exercises are key component to our unique, hands-on Head First approach.
- Bend your mind around blocks and methods
- Build blocks into larger structures
- Do heavy lifting easily with blocks
- Get more done with less code
This open book is licensed under a Creative Commons License (CC BY). You can download Using Blocks in Ruby ebook for free in PDF format (2.0 MB).
Table of Contents
Section 1
Blocks are mind-bending stuff. But stick with it!
Section 2
Defining a method that takes blocks
Section 3
Your first block
Section 4
Flow of control between a method and block
Section 5
Calling the same method with different blocks
Section 6
Calling a block multiple times
Section 7
Block parameters
Section 8
Using the "yield" keyword
Section 9
Block formats
Section 10
The "each" method, step-by-step
Section 11
DRYing up our code with "each" and blocks
Section 12
Blocks and variable scope
Section 13
Using "each" with the "refund" method
Section 14
Using "each" with our last method
Section 15
Our complete invoicing methods
Section 16
We've gotten rid of the repetitive loop code!
Section 17
Utilities and appliances, blocks and methods