Sunday, January 17, 2010

Ruby and AOP

Right now I am learning Ruby, I focus on Dave Thomas' famous pickaxe Ruby book, last week I just finished the chapter 24: "Metaprogramming".Now I learn the power of Ruby Metaprogramming, right now it is still quite difficult for me, I am sure I will read this chapter over and over in future.

Today I want to discuss my impression of AOP concept in Ruby.
I learned AOP from java, in java you have to use AspectJ or Dynamic Proxy implement AOP. But comapre to Java, I realize Ruby support AOP in the lanuage level:
  • Classes are open: you add new method or attribute for any existing class, or replace the method with new one.
  • Module and Mixin: you can easily add new functionalities by Mixin the module.
  • MetaProgramming: you can dynamically define a method on the fly, using hook method as call back, easily implement before, after and around advice
I feel by using the above technologies, it is quite easy using AOP concept in Ruby, for example you can easily implement a Logging Aspect in a Module, then get it mixin in your application code.That is separating of concern, which is the essence of AOP.
So I will say AOP is built in support in Ruby language level.

Today I found a blog called "Does Ruby need AOP?", the author argue that Ruby metaprogramming is not AOP,because he think AOP is all about semantics, while Ruby metaprogramming is too low level. I feel this argument is too academic, from the pragmatic perspective, I think as long as Ruby can do the job of separation of concern, then it is AOP, then who care about the semantics?

Here are some useful links:
  1. another blog about ruby and APO at 4 line of code
  2. slide: AOP of Ruby

No comments:

Post a Comment