我有一颗宝石:
# in /Library/Ruby/Gems/1.8/gems/my_gem-1.0.0/lib/my_gem.rb
module MyGem
def do_stuff
..
end
end
我将它加载到 Rails 中:
# in [rails_root]/config/environment.rb:
config.gem 'my_gem', :version => '1.0.0'
并使用它:
# in [rails_root]/app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
include MyGem
end
但我需要以特定于环境的方式对其进行修补:
# in [rails_root]/config/environments/development.rb:
MyGem.class_eval do
def do_stuff
raise 'Ack! - just testing'
end
end
不幸的是,MyGem
每次请求都会重新加载,所以我的猴子补丁没用。
我检查过load_once_paths
:
ActiveSupport::Dependencies.load_once_paths
# => ["/Library/Ruby/Gems/1.8/gems/my_gem-1.0.0/lib", "other stuff"]
知道如何获得我想要的效果吗?