5. Metaprogramming
In Ruby, everything is open to modification at run time. Classes, modules, and even the behavior of individual objects can all be changed while a program is running. It’s trivial to write code that defines new classes or adds methods to an existing class or object. Virtually nothing is off limits. This so-called “metaprogramming” is one of Ruby’s most powerful features. It’s also one of its most dangerous.
There are a lot of good uses for metaprogramming. Cleaning up redundant code, generalizing a feature to work with more than one class, and creating domain-specific languages are just a few examples. But there are downsides, too. Methods like eval tend to become crutches, since they can be used to solve so many programming ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access