Ara*_*.ck 6 ruby aop exception-handling
我正在尝试使用Ruby中的AOP处理异常.我在这里使用的工具包是Aquarium(http://aquarium.rubyforge.org/).
我编写了一个示例代码,它将尝试映射所写的ApplicationController类的所有后代(子类).
在执行以下程序时,我得到一个SystemStackError(我也尝试使用"ulimit -s"设置堆栈限制).有人请帮我这个!或者有关映射的任何建议:欢迎超类的子类的all_methods ..提前感谢.
require 'aquarium'
include Aquarium::Aspects
class ApplicationController
end
class Abc < ApplicationController
def func
puts "func called"
raise Exception.new # SystemStackError is thrown before reaching place
end
end
#Dummy class
class Def < ApplicationController
end
Aspect.new :after_raising => Exception,
:in_types_and_descendents => "ApplicationController" do |jp, object, *args|
puts "Exception Handling Code"
end
a = Abc.new
a.func
Run Code Online (Sandbox Code Playgroud)
您被要求使用一种仅对像 Java 这样没有模块(或 Scala 的特征)的语言有意义的方法?只要您需要模块文件,您就可以通过使用 self.send :include 或类似的方法在需要的地方包含模块来获得此功能,而无需任何额外的工作。
无论如何,我建议您阅读 Avdi Grimm 的 Exceptional Ruby,以了解 Ruby 中的异常如何工作——同样与 Java 不同——正如已经指出的那样。
Ruby 不需要依赖注入——这完全违背了该语言的哲学。