Working with Yield, Procs, and Blocks

While named methods are typically the core of an application, sometimes you want to be able to manipulate your logic more flexibly. Procs let you put logic, including methods, into variable-like structures that you can pass through your program and call explicitly or through the yield keyword. Like methods, Procs can take arguments and return values.

This is bread and butter for a Rubyist, but if you don’t know Ruby, this subject can be a bit confusing at first. For non-Rubyists, Using Procs may be an easier if usually more verbose place to start with these concepts.

Blocks let you reuse code without creating formal methods, once you learn how the syntax works. (You’ve already been using them, but they ...

Get Programming Crystal now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.