LoginSignup
7
7

More than 5 years have passed since last update.

Rails3.2.9 development環境でリクエストの度にプライベートGemをリロードする方法

Posted at

Rails用のgemを手元で作っている時にコードを書きなおす度にWebrickを再起動してgemを読みなおすのは非常に手間。なのでこれを回避する方法

例えばexampleという名前のgemを作っていてExample::MyClassをリクエストの度にリロードをしたい場合enviroments/development.rbに以下を追加

enviroments/development.rb
  # requestが来た場合のcallbackを定義
  ActionDispatch::Callbacks.before do
    # Example::MyClassが定義されている場合は削除する
    if Object.const_defined?(:Example)
      if Example.const_defined?(:MyClass)
        Example.send(:remove_const, :MyClass)
       end
    end
    # example/my_classがrequireされている場合は削除して再度requireする
    $LOADED_FEATURES.delete_if {|s| s.include? "example/my_class"}
    require 'example/my_class'
  end
7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7