# Adding aspects that modify return values?

**URL:** <https://www.ruby-forum.com/t/adding-aspects-that-modify-return-values/112539>\
**Category:** Ruby\
**Created:** [August 20, 2007, 7:06pm UTC](https://www.ruby-forum.com/t/adding-aspects-that-modify-return-values/112539 "2007-08-20T19:06:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mario\_TSL](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@Mario\_TSL](https://www.ruby-forum.com/u/Mario_TSL)\
**Post date:** [August 20, 2007, 7:06pm UTC](https://www.ruby-forum.com/t/adding-aspects-that-modify-return-values/112539/1 "2007-08-20T19:06:49Z")

</div>

I am trying to use ‘aspectr’ to add aspects to Rails FormHelpers like  
‘text\_field’. I want the aspects to be able to intercept the return  
value and modify it so that the original method call ‘text\_field’  
returns the value that I modified in the aspect.

It appears to me that aspects do not allow return values to be modified.  
Too bad. This would have perfectly met the need I am after. I want to  
add extra markup surrounding the ‘input’ markup in order to standardize  
my views in a DRY way.

I don’t really want to fool with the original FormHelpers because I want  
my add-on to be modular and separate. I considered inheriting to create  
a new FormHelper, but thought better of it, since aspects seemed more  
suited.

Am I wrong about aspects? Can they intercept and modify return values?  
If not, what are the alternatives?

I am working on a library that will DRY up my presentation logic.  
Eventually, if I can overcome these hurdles, I intend to open source the  
library.

Thanks Much!  
Mario T. Lanza

---

<div class="post-metadata">

**Author:** ![Mario\_TSL](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@Mario\_TSL](https://www.ruby-forum.com/u/Mario_TSL)\
**Post date:** [August 26, 2007, 8:35pm UTC](https://www.ruby-forum.com/t/adding-aspects-that-modify-return-values/112539/2 "2007-08-26T20:35:27Z")

</div>

On 8/20/07, Mario T. Lanza [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> library.
> 
> ## Thanks Much! Mario T. Lanza
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

I’m not sure about aspectr, but the convention for most AOP frameworks  
is  
that “after” advice, which runs after the method, in this case, cannot  
change the value that is actually returned to the caller. Instead, you  
have  
to use “around” advice, where you explicit invoke the wrapped method (in  
a  
way that is defined by the AOP toolkit), modify the value returned, as  
desired, and finally return the resulting value to the original caller.

This is how Aquarium works (sort-of…), the AOP framework for Ruby that  
I  
released earlier this week. ([http://aquarium.rubyforge.org](http://aquarium.rubyforge.org)) In this  
case,  
you could do what you want this way:

# inside your FormHelpers “helper” file

around :types =\> /FormHelper$/, :methods =\> […?] do | join\_point,  
\*args |  
result = proceed # Call the “wrapped” method  
result += “…”  
end

I believe that aspectr has similar behavior.

Currently, Aquarium does allow “after” (and the more restrictive  
“after\_returning”) advice to modify the value returned to the caller:

after\_returning :types =\> /FormHelper$/, :methods =\> […?] do |  
join\_point,  
\*args |  
join\_point.context.returned\_value = “…” +  
join\_point.context.returned\_value + “\<some-more-mark-up…”  
end

However, this may change, so I wouldn’t count on this behavior in  
surviving…

dean

---

<div class="post-metadata">

**Author:** ![Mario\_TSL](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@Mario\_TSL](https://www.ruby-forum.com/u/Mario_TSL)\
**Post date:** [August 27, 2007, 12:27am UTC](https://www.ruby-forum.com/t/adding-aspects-that-modify-return-values/112539/3 "2007-08-27T00:27:48Z")

</div>

I could only find :before and :after points in AspectR. I am new to  
aspects, so perhaps I have overlooked how it might be done in AspectR.  
Nevertheless, your Aquarius gem looks promising. I’ll give it a look.

Thank you!  
Mario
