Ruby Metaprograming
Posted by jgrant, 2006-10-15 19:10:00
Ola Bini's Ruby Metaprogramming techniques is a great summary of the meta-programming techniques in common use today by many of the interesting Ruby libraries.
Namely:
- Use the singleton-class
- Write DSL's using class-methods that rewrite subclasses
- Create classes and modules dynamically
- Use method_missing to do interesting things
- Dispatch on method-patterns
- Replacing methods
- Use NilClass to implement the Introduce Null Object refactoring
- Learn the different versions of eval
- Introspect on instance variables
- Create Procs from blocks and send them around
- Use binding to control your evaluations
- Other good resources on this topic:*
- Why's eye-opening: 'Seeing Metaclasses Clearly' from a while back.
- 'Why's Poignant Guide to Ruby' - Chapter 6
- 'Eval-less Metaprogramming' on Redhanded
- Liquid Development's 'The Way of Meta'
