O'Reilly logo

Mastering Ruby Closures by Benjamin Tan Wei Hao

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Wrapping Up

In this chapter, we took a deep dive into blocks and how the yield keyword and block_given? method give you the tools you need to write your own methods that leverage blocks.

You learned that, in addition to enumeration, blocks can help with managing resources such as file handles and database connections, and also provide a succinct way to initialize objects.

Blocks, sprinkled with some meta-programming goodness, are also the key to building beautiful DSLs. You’ve learned that instance_eval, with its ability to switch the context of the object based on where it has been invoked, is responsible for many of the DSLs, such as those found in the Rails router and Active Record.

Now that you worked through the examples and exercises, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required