Chapter 3. Mastering the Dynamic Toolkit

If you’ve done even a little bit of Ruby, you probably have a sense of the great flexibility and power that it offers as a language. This chapter is designed to underscore that point, specifically by showing you what can be accomplished when you unleash the power of Ruby onto itself. When you hear that everything is an object in Ruby, it’s easy to forget that classes, modules, and methods all fall into that category as well. With enough imagination, we can think of all sorts of interesting applications that fall out of this elegant design.

Take the fact that all of our programmatic constructs can be represented as first-order data objects and combine it with the ability to modify any of them at runtime. Then mix in the idea that everything from defining a new function to calling a method that does not exist can be detected and handled by custom code. Top this off with first-rate reflection capabilities and you’ll find that Ruby is a perfect foundation for writing highly dynamic applications.

On the surface, these ideas may seem a bit esoteric or academic in nature. But when you get down to it, there are a lot of practical uses for having such a high degree of flexibility baked into Ruby. Because Ruby’s dynamic nature is a huge part of what makes the language what it is, you’ll find no shortage of real examples in this chapter. These run the gamut from dynamic interface generation to safely modifying preexisting code at runtime. But to get your ...

Get Ruby Best Practices 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.