[Nitro] Aspects

George Moschovitis george.moschovitis at gmail.com
Sun Feb 25 11:39:41 EST 2007


erm... please give me some time to ...comprehend this ;-)

-g.

On 2/24/07, transfire at gmail.com <transfire at gmail.com> wrote:
>
> Playing around some. You might find this interesting:
>
>
> class Class
>
>   alias :create :new
>
>   def new(*a,&b)
>     obj = allocate
>     obj.extend advice
>     obj.send(:initialize,*a,&b)
>     return obj
>   end
>
> end
>
>
> class Module
>
>   def advice(&block)
>     @advice ||= Advice.new(self)
>     @advice.module_eval(&block) if block
>     @advice
>   end
>
> end
>
> class Advice < Module
>   class << self
>     alias :new :create
>   end
>
>   def initialize(base)
>     base.ancestors[1..-1].reverse.each do |anc|
>       include anc.advice
>     end
>   end
> end
>
>
>
> # example
>
>
> class X
>   def y
>     "y"
>   end
>
>   advice do
>     def y
>       '{' + super + '}'
>     end
>   end
>
> end
>
>
> class Z < X
>
>   def y
>     super + "!"
>   end
>
>   advice do
>     def y
>       '[' + super + ']'
>     end
>   end
>
> end
>
> puts X.new.y
> puts Z.new.y
>
> _______________________________________________
> Nitro-general mailing list
> Nitro-general at rubyforge.org
> http://rubyforge.org/mailman/listinfo/nitro-general
>



-- 
http://blog.gmosx.com
http://cull.gr
http://www.joy.gr
http://nitroproject.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/nitro-general/attachments/20070225/d24ed9d1/attachment-0001.html 


More information about the Nitro-general mailing list