January 2024
Intermediate to advanced
718 pages
20h 15m
English
This section briefly describes one of Ruby’s particular strengths—blocks. A code block is a chunk of code you can pass to a method, as if the block were another parameter. This is an incredibly powerful feature, allowing Ruby methods to be extremely flexible. One of this book’s early reviewers commented at this point: “This is pretty interesting and important, so if you weren’t paying attention before, you should probably start now.” We still agree.
Syntactically, code blocks are chunks of code that can be delimited one of two ways: between braces or between do and end. This is a code block at the end of a message call:
| | foo.each { puts "Hello" } |
This is also a code block at the end of a message call:
| | foo.each do |
| | club.enroll ... |
Read now
Unlock full access