Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1

2 | Chapter 1: Foundational Techniques


Metaprogramming is used in many languages, but it is most popular in dynamic lan-
guages because they typically have more runtime capabilities for manipulating code as
data. Though reflection is available in more static languages such as C# and Java, it is
not nearly as transparent as in the more dynamic languages such as Ruby because the
code and data are on two separate levels at runtime.


Introspection is typically done on one of two levels.Syntactic introspectionis the low-
est level of introspection—direct examination of the program text or token stream.
Template-based and macro-based metaprogramming usually operate at the syntactic
level.


Lisp encourages this style of metaprogramming by usingS-expressions(essentially a
direct translation of the program’s abstract syntax tree) for both code and data.
Metaprogramming in Lisp heavily involvesmacros, which are essentially templates
for code. This offers the advantage of working on one level; code and data are both
represented in the same way, and the only thing that distinguishes code from data is
whether it is evaluated. However, there are some drawbacks to metaprogramming at
the syntactic level. Variable capture and inadvertent multiple evaluation are direct
consequences of having code on two levels of abstraction in the source evaluated in the
same namespace. Although there are standard Lisp idioms for dealing with these prob-
lems, they represent more things the Lisp programmer must learn and think about.


Syntactic introspection for Ruby is available through the ParseTree library, which
translates Ruby source into S-expressions.*An interesting application of this library
is Heckle,†a test-testing framework that parses Ruby source code and mutates it,
changing strings and flippingtruetofalseand vice versa. The idea is that if you have
good test coverage, any mutation of your code should cause your unit tests to fail.


The higher-level alternative to syntactic introspection issemantic introspection,or
examination of a program through the language’s higher-level data structures.
Exactly how this looks differs between languages, but in Ruby it generally means
working at the class and method level: creating, rewriting, and aliasing methods;
intercepting method calls; and manipulating the inheritance chain. These techniques
are usually more orthogonal to existing code than syntactic methods, because they
tend to treat existing methods as black boxes rather than poking around inside their
implementations.


Don’t Repeat Yourself


At a high level, metaprogramming is useful in working toward theDRY principle
(Don’t Repeat Yourself). Also referred to as “Once and Only Once,” the DRY prin-
ciple dictates that you should only need to express a particular piece of informa-
tion once in a system. Duplication is usually unnecessary, especially in dynamic


*http://www.zenspider.com/ZSS/Products/ParseTree/
http://rubyforge.org/projects/seattlerb

Free download pdf