# Ruby and AOP

**URL:** <https://www.ruby-forum.com/t/ruby-and-aop/93333>\
**Category:** Ruby\
**Created:** [March 31, 2006, 4:56am UTC](https://www.ruby-forum.com/t/ruby-and-aop/93333 "2006-03-31T04:56:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [March 31, 2006, 4:56am UTC](https://www.ruby-forum.com/t/ruby-and-aop/93333/1 "2006-03-31T04:56:45Z")

</div>

I’m reading “The Pragmattic Programmer” right now and I’m at the part  
about orthogonality. The authors mention AOP and give an example  
involving log writing wherein you can “weave in” some AOP (“Aspect  
Oriented Programming” for those not in the know) sexiness and log  
writing occurs (the converse behaves as you might expect)\*\*.

It seems to me that this behavior is _sort_ of like a mixin – i.e. you  
can mix it in for a new behavior. However, AOP code seems to be  
triggered when certain actions happen.

Does this sound roughly correct to those who have AOP experience? Have  
any of you used AOP in your experience, and if so, where has it been  
most beneficial?

Thank you,

James H.

\*\* For those interested, yes a Ruby AOP project exists:  
[http://aspectr.sourceforge.net/](http://aspectr.sourceforge.net/)

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [March 31, 2006, 5:45am UTC](https://www.ruby-forum.com/t/ruby-and-aop/93333/2 "2006-03-31T05:45:23Z")

</div>

James H. wrote:

> I’m reading “The Pragmattic Programmer” right now and I’m at the part  
> about orthogonality. The authors mention AOP and give an example  
> involving log writing wherein you can “weave in” some AOP (“Aspect  
> Oriented Programming” for those not in the know) sexiness and log  
> writing occurs (the converse behaves as you might expect)\*\*.

It’s actually pretty much like this with aspectj (in java land). I  
haven’t used anything except vanilla aspectj, the new version is  
combined with AspectWorkz and uses @Annotation style java - something  
that so far I’ve not gotten into

> It seems to me that this behavior is _sort_ of like a mixin – i.e.  
> you can mix it in for a new behavior. However, AOP code seems to be  
> triggered when certain actions happen.

It’s not like a mixin in implementation

> Does this sound roughly correct to those who have AOP experience?  
> Have any of you used AOP in your experience, and if so, where has it  
> been most beneficial?

The proto-typical use case for AOP is logging, indeed the AspectJ team  
did a presentation where they demonstrated how much cleaner Apache  
Tomcat could be with an AOP approach towards logging - it’s quite an  
eye-opener how much code can be replaced by a single aspect. It’s all  
good in theory, but in practice (again in java land), the tools are  
slightly different ajc instead of javac, decding on runtime or loadtime  
weaving etc etc. Basically if you’re already bogged down with  
frameworks from hell (I’m looking at you JSF), then adding AspectJ/AOP  
in can be an extra thing to try and keep straight.

That said, you can achieve some wonderful results. I added an auditing  
feature to a project where all methods that changed the state of data  
caused an aspect to fire and to record the persons name/change etc with  
a single aspect (\>100 lines of Java/AspectJ) - this made the code much  
cleaner as we didn’t have calls to an audit service before executing the  
business logic, we just let the aspect take care of that. I’ve also  
used it for security/authentication, again ensure the user is logged in  
before performing an action - another (relatively) straightforward  
aspect.

One thing I’ve seen but never coded up is using aspects for caching (the  
example used JBoss cache and I didn’t have the willpower to download yet  
another app server just to test something)

So erm, yeah aspects are quite cool, certainly worth looking at for a  
set of problems. IOC sort of stole their thunder and has negated the  
press AOP was getting for a while, but now most IOC frameworks use  
aspects internally.

Kev

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [March 31, 2006, 6:27am UTC](https://www.ruby-forum.com/t/ruby-and-aop/93333/3 "2006-03-31T06:27:53Z")

</div>

Another distinction is that AspectJ and related technologies use a  
“push”  
approach to weave aspects into different join-points in the code. In the  
case of a mixin, it’s more of a “pull” approach where you have to walk  
up to  
the type that you want to add the mixin functionality to.

I spent a lot of time a few years ago implementing a runtime  
aspect-weaving  
extension to the CLR - that had a lot of potential - you could weave  
code  
into the middle of a running system, and then yank it back out when you  
were  
done. Great for doing things like injecting diagnostic probes into a  
corrupted system to perform post-mortem diagnosis.

-John

> **[John Lam's Blog](https://www.iunknown.com/)**
>
> Since 1997

Yes, it’s like a mixin but on more than a ‘class’ level. It’s

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [March 31, 2006, 7:31am UTC](https://www.ruby-forum.com/t/ruby-and-aop/93333/4 "2006-03-31T07:31:48Z")

</div>

> So erm, yeah aspects are quite cool, certainly worth looking at for a  
> set of problems. IOC sort of stole their thunder and has negated the  
> press AOP was getting for a while, but now most IOC frameworks use  
> aspects internally.

Pardon my naivity, but what is IOC?

Thank you,

James H.

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [March 31, 2006, 6:03am UTC](https://www.ruby-forum.com/t/ruby-and-aop/93333/5 "2006-03-31T06:03:38Z")

</div>

On 3/30/06, James H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Does this sound roughly correct to those who have AOP experience? Have  
> any of you used AOP in your experience, and if so, where has it been  
> most beneficial?

Yes, it’s like a mixin but on more than a ‘class’ level. It’s  
cross-cutting(to use the jargon) across class hierarchies.

Basically AOP allows a further and different separation of concerns in  
a software system. Think of transactions and security as better uses  
of AoP. An object should be able to participate in a transaction  
without ever knowing it’s been in one. Log writing is the ‘Hello  
World’ of AoP.

I’ve used AoP for Logging and exception tracing at runtime of working  
systems.

Ron Bodkin and Ramnivas Laddad have excellent blogs on AoP.

hth,

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [March 31, 2006, 8:29am UTC](https://www.ruby-forum.com/t/ruby-and-aop/93333/6 "2006-03-31T08:29:33Z")

</div>

On 3/31/06, James H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > So erm, yeah aspects are quite cool, certainly worth looking at for a  
> > set of problems. IOC sort of stole their thunder and has negated the  
> > press AOP was getting for a while, but now most IOC frameworks use  
> > aspects internally.
> 
> Pardon my naivity, but what is IOC?

IOC = Inversion Of Control. See Marting Fowler’s article for a good  
explanation: [Inversion of Control Containers and the Dependency Injection pattern](http://www.martinfowler.com/articles/injection.html)

There are several implementations in Ruby, Needle for example:  
[http://needle.rubyforge.org/](http://needle.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![sjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sjames](https://www.ruby-forum.com/u/sjames)\
**Post date:** [March 31, 2006, 8:29am UTC](https://www.ruby-forum.com/t/ruby-and-aop/93333/7 "2006-03-31T08:29:35Z")

</div>

Hi,

James H. schrieb:

> Pardon my naivity, but what is IOC?

Inversion of Control. Read Martin F.'s article “Inversion of Control  
Containers and the Dependency Injection pattern” at

> **[Inversion of Control Containers and the Dependency Injection
	pattern](https://www.martinfowler.com/articles/injection.html)**
>
> Explaining the Dependency Injection pattern, by contrasting it with Service Locator. The choice between them is less important than the principle of separating configuration from use.

Lutz
