# How odd is Ruby code allowed to be?

**URL:** <https://www.ruby-forum.com/t/how-odd-is-ruby-code-allowed-to-be/64200>\
**Category:** Ruby\
**Created:** [June 23, 2006, 12:16am UTC](https://www.ruby-forum.com/t/how-odd-is-ruby-code-allowed-to-be/64200 "2006-06-23T00:16:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![roggan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8c91f0/32.png) [@roggan](https://www.ruby-forum.com/u/roggan)\
**Post date:** [June 23, 2006, 12:16am UTC](https://www.ruby-forum.com/t/how-odd-is-ruby-code-allowed-to-be/64200/1 "2006-06-23T00:16:48Z")

</div>

Ok, strange question maybe.  
Anyway, Im new to Ruby and I decided to make a small Aop lib just to  
learn all the nice things about ruby…

so in my lib I can define an aspect like this:

`
class MyAspect < Aspect
   apply_to_types SomeClass, String, FooBar
pointcut(“some_method”,“some_prop=”,“some_prop”,“length”).with(
LoggingInterceptor.new )
end
`

Would this be considered valid in the Ruby world or is this just as  
crazy as I think it look?

The reason I decided to try to make the aspect as a class even though it  
isnt intended to be instanciated is that since it inherits my Aspect  
base, I get access to my dsl’ish methods in the scope of the class.

I can then register this class in my Aop engine which extracts the info  
out of the static structure.

So is it valid or just madness?

//Roger

---

<div class="post-metadata">

**Author:** ![roggan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8c91f0/32.png) [@roggan](https://www.ruby-forum.com/u/roggan)\
**Post date:** [June 23, 2006, 12:58am UTC](https://www.ruby-forum.com/t/how-odd-is-ruby-code-allowed-to-be/64200/2 "2006-06-23T00:58:42Z")

</div>

On 6/22/06, Roger J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Would this be considered valid in the Ruby world or is this just as  
> crazy as I think it look?

Looks valid to me though I don’t know how you’d implement it.

In any case, take your .rb file and run a syntax check on it - ruby -c  
yourfile.rb

---

<div class="post-metadata">

**Author:** ![roggan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8c91f0/32.png) [@roggan](https://www.ruby-forum.com/u/roggan)\
**Post date:** [June 23, 2006, 2:30am UTC](https://www.ruby-forum.com/t/how-odd-is-ruby-code-allowed-to-be/64200/3 "2006-06-23T02:30:23Z")

</div>

Hi –

On Fri, 23 Jun 2006, Roger J. wrote:

> pointcut(“some\_method”,“some\_prop=”,“some\_prop”,“length”).with(
> 
> I can then register this class in my Aop engine which extracts the info  
> out of the static structure.
> 
> So is it valid or just madness?

It looks to me a bit like you’re using a class where you should use an  
instance. In other words, you’re treating inheritance as  
instantiation.

Why not:

aspect = Aspect.new do |a|  
a.apply\_to\_types SomeClass, String, FooBar  
a.pointcut(“some\_method”,…).with(LoggingInterceptor.new)  
a.register\_with\_aop\_engine  
end

Mind you, I know little enough about AOP that this may be completely  
on the wrong track. But it seems like what you want is an aspect, and  
you’ve got an Aspect class, so aspect = Aspect.new seems a good place  
to start.

David

–  
David A. Black (removed\_email\_address@domain.invalid)  
Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))

See what the readers are saying about “Ruby for Rails”!

> **[Ruby on Rails Framework | Ruby Power and Light](https://www.rubypowerandlight.com/)**
>
> Main aspects of the Ruby language with focus on Rails - the most widespread and powerful Web Application Framework of the language, which implements the MVC pattern.

---

<div class="post-metadata">

**Author:** ![roggan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8c91f0/32.png) [@roggan](https://www.ruby-forum.com/u/roggan)\
**Post date:** [June 23, 2006, 5:54pm UTC](https://www.ruby-forum.com/t/how-odd-is-ruby-code-allowed-to-be/64200/4 "2006-06-23T17:54:46Z")

</div>

On 6/22/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Why not:
> 
> aspect = Aspect.new do |a|  
> a.apply\_to\_types SomeClass, String, FooBar  
> a.pointcut(“some\_method”,…).with(LoggingInterceptor.new)  
> a.register\_with\_aop\_engine  
> end

Like others, I’m not sure how the implementation of the  
apply\_to\_types, pointcut and register\_with\_aop\_engine will go (I’m  
sure there’s some… I’m just not worrying about it), but I agree with  
David that an instance of the Aspect class is probably what you really  
want. I’d clean the syntax up (“clean” being just my opinion) a little  
more like:

aspect(SomeClass, String, FooBar) do  
pointcut :some\_method,  
:some\_prop=,  
:some\_prop,  
:length  
with LoggingInterceptor.new  
end

Implementation of the builder:

def aspect(\*classes, &definition)  
AspectBuilder.instance.build(\*classes, &definition)  
end

class AspectBuilder  
include Singleton

```
def build(*classes, &definition)
  @aspect = Aspect.new
  @methods = []
  @aspect.apply_to_types *classes
  instance_eval &definition
  @aspect.register_with_aop_engine
end

def pointcut(*methods)
  @methods += methods
end

def with(interceptor)
  @aspect.pointcut(*@methods).with(interceptor)
  @methods = []
end

```

end

Sample implementation of the Aspect class (just stubbing here):

class Aspect  
def apply\_to\_types(\*classes)  
@classes = classes  
end

```
def pointcut(*methods)
  cut = PointCut.new(*methods)
  (@cuts ||= []) << cut
  cut
end

def register_with_aop_engine
  classes = @classes.size == 1 ? @classes[0] :
    [@classes[0..-2].join(', '), @classes[-1]].join(' and ')

  puts "Adding an aspect on #{classes} with the following cuts:"
  @cuts.each{ |cut| puts "\t#{cut}" }
end

class PointCut
  def initialize(*methods)
    @methods = methods
  end

  def with(interceptor)
    @interceptor = interceptor
  end

  def to_s
    methods = @methods.size == 1 ? @methods[0] :
      [@methods[0..-2].join(', '), @methods[-1]].join(' and ')

    "Cutting #{methods} with a #{@interceptor.class}"
  end
end

```

end

Output of the above built aspect with the Aspect stub as above and  
simple classes for SomeClass, FooBar and LoggingInterceptor:

Adding an aspect on SomeClass, String and FooBar with the following  
cuts:  
Cutting some\_method, some\_prop=, some\_prop and length with a  
LoggingInterceptor

Jacob F.

---

<div class="post-metadata">

**Author:** ![roggan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8c91f0/32.png) [@roggan](https://www.ruby-forum.com/u/roggan)\
**Post date:** [June 23, 2006, 6:23pm UTC](https://www.ruby-forum.com/t/how-odd-is-ruby-code-allowed-to-be/64200/5 "2006-06-23T18:23:59Z")

</div>

On 23-jun-2006, at 0:16, Roger J. wrote:

> Ok, strange question maybe.

I think you might want emulated named arguments via a hash for this  
kind of stuff

pointcut :in =\> :some\_method, :of =\> [:some\_prop=, :some\_prop], :with  
=\> LoggingInterceptor.new

---

<div class="post-metadata">

**Author:** ![roggan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8c91f0/32.png) [@roggan](https://www.ruby-forum.com/u/roggan)\
**Post date:** [June 23, 2006, 11:39am UTC](https://www.ruby-forum.com/t/how-odd-is-ruby-code-allowed-to-be/64200/6 "2006-06-23T11:39:54Z")

</div>

On 6/23/06, Roger J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> pointcut(“some\_method”,“some\_prop=”,“some\_prop”,“length”).with(  
> LoggingInterceptor.new )  
> end

Looks like normal code to me, syntax and semantics, not pragmatics.

Would this be considered valid in the Ruby world or is this just as

> crazy as I think it look?

Did you write it? In that case, why did you write it in that way, or the  
other way round  
rewrite it differenly and compare.  
In case you did not, no I do not think it looks crazy.

Now switching to the pragmatics you are worried about.

The reason I decided to try to make the aspect as a class even though it

> isnt intended to be instanciated is that since it inherits my Aspect  
> base, I get access to my dsl’ish methods in the scope of the class.

If you are worried about your design because you do not like the  
resulting  
code, I personally, and I might get strong opposition, think that you  
are on  
the path to perfection.  
It is a long and winding road, and of course it is assympthotic ☹

I can then register this class in my Aop engine which extracts the info

> out of the static structure.
> 
> So is it valid or just madness?

No of course it is not madness, but maybe you should rethink the design  
and  
think about David’s answer below (sorry if I messed up the posting  
position  
).

Cheers  
Robert

//Roger

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![roggan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8c91f0/32.png) [@roggan](https://www.ruby-forum.com/u/roggan)\
**Post date:** [June 24, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/how-odd-is-ruby-code-allowed-to-be/64200/7 "2006-06-24T19:08:10Z")

</div>

Ok, thanks for all the great feedback.

here is how it turned out:

```
Aop.define_aspect do
	self_register

	match_by_signature "Some*" #matches by class names
	match_by_class String #matches by a specific Class

	pointcut_by_signature "some_method", "some_prop="
	with LoggingInterceptor.new

	pointcut_by_signature "length"
	with LoggingInterceptor.new, TestInterceptor.new

	#mixins that will be "include"'ed to the types matched by the aspect
	mixin MyMixin, MyOtherMixin
end
```

I cant say anything else than Im starting to fall in love with ruby.  
the implementation of blocks is just too bloody slick in ruby.

with this I can get a totally declarative approach to defining aspects.  
and the dsl can easily be extended by just adding methods to my Aspect  
class.

any comments, does it look somewhat ok?  
Ive only been playing with ruby for about a week now so I dont quite  
know when Im abusing the language or not.

//Roger
