Ruby中AOP实现方法

转自:http://blackanger.blog.51cto.com/140924/40810

Ruby是如何实现AOP的。。。只涉及AOP中的一个方面,拦截(Interception)
[Copy to clipboard] [ - ]
CODE:
# foo.rb
class Foo
def bar
"bar"
end
end


[Copy to clipboard] [ - ]
CODE:
# foo_advice.rb
class Foo
alias old_bar bar
def bar
"{[< "+ old_bar+ " >]}”
end
end

利用alias来拦截方法。。。来个稍微好点的:

[Copy to clipboard] [ - ]
CODE:
# foo_advice.rb
class Foo
old_bar = self.instance_method(:bar)
define_method(:bar) do
"{[< " + old_bar.bind(self).call() + " >]}"
end
end

为什么这个好呢?
用了define_method虽然不是那么清晰,但是代码却很健壮。防止不同域定义的同名对象冲突.

2.Ruby Inspection
许多反射方法:

[Copy to clipboard] [ - ]
CODE:
s="foo"
local_variables
global_variables
s.class
s.display
s.inspect
s.instance_variables
s.methods
s.private_methods
s.protected_methods
s.public_methods
s.singleton_methods
s.method(:size).arity
s.method(:replace).arity



继续:

[Copy to clipboard] [ - ]
CODE:
# List all the classes
ObjectSpace.each_object(Class) do |o|
puts o.inspect
end
# List all the modules
ObjectSpace.each_object(Module) do |o|
puts o.inspect
end
# List all the instances of class String
ObjectSpace.each_object(String) do |o|
puts o.inspect
end

回顾一下java的AOP概念的实现,无非就是利用java的反射机制,得到需要拦截的方法,然后增加切面。。。,Ruby 也一样。。。

问题:如何取消定义本类的所有实例变量?除了@attributes.下面例子可能次一点

[Copy to clipboard] [ - ]
CODE:
#a.rb
class A
   def test
      @var=1
      @attr = 2
      i = 1
      j = 2
     local_variables
   end
end

irb(main):111:0> a = A.new
=> #<A:0xcf0cab0>
irb(main):112:0> a.test
=> ["i", "j"]
irb(main):113:0> a.instance_variables
=> ["@var", "@attr"]


[Copy to clipboard] [ - ]
CODE:
#a_advice.rb
class A
   old_test = self.instance_method(:test)
   define_method(:test) do
      @attr = 2
       i = 1
       local_variables
    end
end

irb(main):122:0> a = A.new
=> #<A:0xcef92f8>
irb(main):123:0> a.test
=> ["old_test", "i"]
irb(main):124:0> a.instance_variables
=> ["@attr"]

你可能感兴趣的:(AOP,Blog,J#,Ruby)