Can Rails console reload modules under lib?

后端 未结 4 634
醉话见心
醉话见心 2021-01-30 12:39

I have a module in my Rails project under lib. I run \'rails c\' and do some experimenting in the console. I make a change to the module under lib, type \'reload!\' from the c

4条回答
  •  忘了有多久
    2021-01-30 13:05

    this is the monkeypatch that could help you, paste this in rails console (or you could put this code in a monkeypatch file - although I don't recommend monkeypatching Object with an utility method):

    class Object
      def self.reload_myself!
        method = (self.instance_methods(false) + self.methods(false)).select{|method| method.to_s[0] =~ /[A-Za-z]/}.last
        if method
          if self.instance_methods(false).index method
            method = self.instance_method(method)
          elsif
            method =  self.method(method)
          end
    
          if (method.source_location)
            source_location = method.source_location[0]
            puts "reloading: #{source_location}"
            load "#{source_location}"
          else
            puts "could not reload #{self.name}"
          end
        end
      end
    end
    

    and you can call

    reload_myself!
    

    on any object to reload its source code.

提交回复
热议问题