MΓ³dulo 25 Β· Blocks, Procs & Lambdas β LecciΓ³n 1 de 6 Β· ~9 min
Methods That Take Blocks
Blocks are Ruby's superpower β and you've only used them
You've passed blocks to each, map, and select since module 02. Now you'll write methods that accept them. This is the skill that makes Ruby feel like Ruby: methods that hand control back to their caller mid-execution. File.open(path) { |f| ... }, transaction { ... }, 5.times { |i| ... } β every one is a method yielding to a block you supply.
In an interview, say: "A block is a chunk of caller-supplied code a method can run via
yield. It's how Ruby does the template-method pattern without ceremony β the method owns the when, the block owns the what."