SystemStackError in Ruby Exception Aquarium Processing (Aspect Oriented Programming)

I am trying to handle exceptions using AOP in Ruby. The toolbox I used here is the Aquarium (http://aquarium.rubyforge.org/).

I wrote an example code that will try to display all the child (subclasses) of the ApplicationController class.

When I run the following program, I get a SystemStackError (I also tried to set the stack limit using "ulimit -s"). Someone please help me with this !. Or any suggestions for mapping: all_methods of subclasses of the superclass are welcome .. Thanks in advance.

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 
+6
source share
2 answers

Have you been instructed to use a method that only makes sense for languages ​​like Java that don't have modules (or Scala attributes)? You can get this without any extra work by including the module where you need it, with self.send: include or the like if you need a module file.

In any case, I suggest you read Avdi Grimm Exceptional Ruby to understand how exceptions work in Ruby - again, not the same as Java - as stated.

Ruby does not need dependency injection - it is completely contrary to the philosophy of the language.

+1
source

You can use my little jewel - aspector to achieve this.

Using an aspectrator is regular ruby ​​classes in which you define the logic before / after / around the execution of a method. Aspects can be tested independently and applied to classes. I gave a code example below, but a complete example can be found here

 class ExceptionHandler < Aspector::Base around options[:methods] do |proxy, *args, &block| begin proxy.call *args, &block rescue Exception => e puts "Exception Handling Code" end end end ExceptionHandler.apply Abc, :methods => Abc.instance_methods a = Abc.new a.func 
+1
source

Source: https://habr.com/ru/post/911747/


All Articles