以下是我正在研究的gem中代码的一般化部分:
module SomeModule
class << self
attr_accessor :procedure
def log_events(*args)
args.flatten!
if args.size == 1
eval "set_trace_func proc {|event, file, line, id, binding, classname| begin; SomeModule.procedure.call(event, file, line, id, binding, classname) if event == #{args[0].inspect}; rescue SystemExit, Interrupt; raise; rescue Exception; end}"
elsif args.size > 1
eval "set_trace_func proc {|event, file, line, id, binding, classname| begin; SomeModule.procedure.call(event, file, line, id, binding, classname) if #{args.inspect}.include?(event); rescue SystemExit, Interrupt; raise; rescue Exception; end}"
else
# ...
end
end
end
end
SomeModule.procedure = lambda {|event, file, line, id, binding, classname| puts "#{event} #{file}.#{line} #{binding} #{classname} #{id}"}
当我打电话时:
SomeModule.log_events 'call', 'return'
在我的Rails应用程序config/environments/development.rb
中的配置块之前,它可以正常工作并继续工作。
但是,当我在类定义的上方或下方的app/controllers/application_controller.rb
中调用它时,由于意外返回(LocalJumpError),Rails在activesupport-3.2.8 / lib / active_support / dependencies.rb:469中自动加载期间死亡
这是日志:
return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/initializable.rb.30 #<Binding:0x007ff7b1403d40> Rails::Initializable::Initializer run
return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/initializable.rb.54 #<Binding:0x007ff7b14039a8> Rails::Initializable run_initializers
return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/application.rb.136 #<Binding:0x007ff7b1403728> Rails::Application initialize!
return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/railtie/configurable.rb.30 #<Binding:0x007ff7b1403480> Rails::Railtie::Configurable::ClassMethods method_missing
call /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb.275 #<Binding:0x007ff7b1402aa8> ActiveSupport::Dependencies::Blamable blame_file!
return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb.277 #<Binding:0x007ff7b1402440> ActiveSupport::Dependencies::Blamable blame_file!
return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb.233 #<Binding:0x007ff7b1401a40> ActiveSupport::Dependencies::Loadable load_dependency
return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb.251 #<Binding:0x007ff7b1409790> ActiveSupport::Dependencies::Loadable require
return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/builder.rb.51 #<Binding:0x007ff7b14093f8> Rack::Builder initialize
return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/builder.rb.40 #<Binding:0x007ff7b1408ac0> Rack::Builder parse_file
return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/server.rb.200 #<Binding:0x007ff7b1408840> Rack::Server app
return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/commands/server.rb.46 #<Binding:0x007ff7b1408598> Rails::Server app
return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/server.rb.301 #<Binding:0x007ff7b14082f0> Rack::Server wrapped_app
return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/server.rb.252 #<Binding:0x007ff7b1408048> Rack::Server start
call /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/server.rb.179 #<Binding:0x007ff7b1407c88> Rack::Server options
return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/server.rb.181 #<Binding:0x007ff7b14078f0> Rack::Server options
Exiting
return /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/commands/server.rb.74 #<Binding:0x007ff7b1406ec8> Rails::Server start
/path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:469:in `load': unexpected return (LocalJumpError)
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:469:in `block in load_file'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:639:in `new_constants_in'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:468:in `load_file'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:353:in `require_or_load'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:502:in `load_missing_constant'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:192:in `block in const_missing'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:190:in `each'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:190:in `const_missing'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/base_controller/menu.rb:2:in `<module:ActiveAdmin>'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/base_controller/menu.rb:1:in `<top (required)>'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `block in require'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:236:in `load_dependency'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/base_controller.rb:2:in `<top (required)>'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/resource_controller/actions.rb:2:in `<module:ActiveAdmin>'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/resource_controller/actions.rb:1:in `<top (required)>'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `block in require'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:236:in `load_dependency'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/resource_controller.rb:2:in `<top (required)>'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/batch_actions.rb:7:in `block in <top (required)>'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/event.rb:25:in `call'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/event.rb:25:in `block in dispatch'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/event.rb:24:in `each'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/event.rb:24:in `dispatch'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/application.rb:179:in `load!'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/application.rb:205:in `routes'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin.rb:79:in `routes'
from /path/to/git/some_app_gemset/config/routes.rb:2:in `block in <top (required)>'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:282:in `instance_exec'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:282:in `eval_block'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/actionpack-3.2.8/lib/action_dispatch/routing/route_set.rb:260:in `draw'
from /path/to/git/some_app_gemset/config/routes.rb:1:in `<top (required)>'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:245:in `load'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:245:in `block in load'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:236:in `load_dependency'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:245:in `load'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/application/routes_reloader.rb:40:in `each'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/application/routes_reloader.rb:40:in `load_paths'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/application/routes_reloader.rb:16:in `reload!'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/application.rb:108:in `reload_routes!'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/reloader.rb:34:in `reload!'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activeadmin-0.5.0/lib/active_admin/reloader.rb:61:in `block in attach!'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:429:in `_run__1105766863370890952__prepare__156232144867029268__callbacks'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `__run_callback'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:385:in `_run_prepare_callbacks'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/actionpack-3.2.8/lib/action_dispatch/middleware/reloader.rb:74:in `prepare!'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/actionpack-3.2.8/lib/action_dispatch/middleware/reloader.rb:48:in `prepare!'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/application/finisher.rb:47:in `block in <module:Finisher>'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/initializable.rb:30:in `instance_exec'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/initializable.rb:30:in `run'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/initializable.rb:55:in `block in run_initializers'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/initializable.rb:54:in `each'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/initializable.rb:54:in `run_initializers'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/application.rb:136:in `initialize!'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /path/to/git/some_app_gemset/config/environment.rb:5:in `<top (required)>'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `block in require'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:236:in `load_dependency'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require'
from /path/to/git/some_app_gemset/config.ru:4:in `block in <main>'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/builder.rb:51:in `instance_eval'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/builder.rb:51:in `initialize'
from /path/to/git/some_app_gemset/config.ru:1:in `new'
from /path/to/git/some_app_gemset/config.ru:1:in `<main>'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/builder.rb:40:in `eval'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/builder.rb:40:in `parse_file'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/server.rb:200:in `app'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/commands/server.rb:46:in `app'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/server.rb:301:in `wrapped_app'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/rack-1.4.1/lib/rack/server.rb:252:in `start'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/commands/server.rb:70:in `start'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/commands.rb:55:in `block in <top (required)>'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/commands.rb:50:in `tap'
from /path/to/.rvm/gems/ruby-1.9.3-p194@some_app_gemset/gems/railties-3.2.8/lib/rails/commands.rb:50:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
知道我做错了什么,或者如何确定set_trace_func的这种用法何时何地应该有效?