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 problems,
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 flipping true to false and 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 is semantic 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.
Pages:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26