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"]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值