27 May 2008

Aquarium 0.4.2: Aspect-Oriented Programming for Ruby

Author: ceefour | Filed under: JRuby, Reviews, Ruby, Tutorials

Aquarium is a framework that implements Aspect-Oriented Programming (AOP) for Ruby. The premise of AOP is that some concerns in an application will cut across the natural object boundaries of the problem domain. Rather than scatter duplicated code in each object to handle the cross-cutting concern, AOP modularizes the specification of which execution points are affected (called join points) and the actions that should be invoked at those points.

New in V0.4.0: Preliminary support for advising Java classes in JRuby! See the discussion here.

See also the RubyForge project page.


Aquarium provides a Domain Specific Language (DSL) with which you can express “aspectual” system behaviour in a modular way, i.e., using a succinct language and without repeating yourself all over your code base!

Imagine you want to trace all invocations of the public, instance methods in all classes whose names end with “Service”. Here’s how you can implement that behavior in Aquarium:

class ServiceTracer
    include Aquarium::Aspects::DSL::AspectDSL
    before :calls_to => :all_methods, :in_types => /Service$/ do |join_point, object, *args|
      log "Entering: #{}##{join_point.method_name}: object = #{object}, args = #{args}"
    after :calls_to => :all_methods, :in_types => /Service$/ do |join_point, object, *args|
      log "Leaving: #{}##{join_point.method_name}: object = #{object}, args = #{args}"

A more succinct implementation of this behavior uses #around advice:

class ServiceTracer
    include Aquarium::Aspects::DSL::AspectsDSL
    around :calls_to => :all_methods, :in_types => /Service$/ do |join_point, object, *args|
      log "Entering: #{}##{join_point.method_name}: object = #{object}, args = #{args}"
      result = join_point.proceed
      log "Leaving: #{}##{join_point.method_name}: object = #{object}, args = #{args}"
      result  # block needs to return the result of the "proceed"!

See the Examples and the API section for more details.

Start Here

$ gem install -y aquarium

See the download page for different options or go directly to Rubyforge download page.

more resources: Aquarium.rubyforge home page.

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Tags: Aquarium, java, JRuby, Programming, Ruby

  • harry seenthing

    wow is nice articles in here i'm glad to be learning in here

  • ceefour

    Thanks Jake! Your suggestions are most welcome.

  • Pingback: Olive Oil

  • Aravindan Ck

    hey, how do you match a controller in the pointcut definition from an aspect in intializer folder of the rails application,
    any suggestions…??