[Nitro] Aspects

transfire at gmail.com transfire at gmail.com
Sat Feb 24 08:40:47 EST 2007


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



More information about the Nitro-general mailing list