How odd is Ruby code allowed to be?

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

On 6/22/06, Roger J. [email protected] 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

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 ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

See what the readers are saying about “Ruby for Rails”!
http://www.rubypowerandlight.com/quotes

On 6/22/06, [email protected] [email protected] 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.

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

On 6/23/06, Roger J. [email protected] 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 :frowning:

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/.


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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs