Metaprogramming is fun! Dynamically modifying a program garners a lot of power to the programmer. In this post I show an example situation which is beautifully solved by metaprogramming. I am a Ruby dilettante.
I wanted to develop a plugin framework for a side project of mine. There is a higher level class, which should delegate methods calls on itself to the plugin implementer class. Although, OOPS has a standard solution for this situation using interfaces but here is the beauty and elegance of metaprogramming in ruby. The code below is the constructor of the higher level class which checks the passed plugin object for interface’s implementation and then defines those methods in itself delegating the actual calls to the passed plugin object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Boy, so simple it is with ruby! And yeah, I love splat operator!