Adding lib to 'config.autoload_paths' in Rails 3 does not autoload my module

后端 未结 6 2025
抹茶落季
抹茶落季 2020-11-28 02:52

I place a file name g.rb in side Rails.root/lib folder The file content is like this:

module Google
end

Then I add

         


        
6条回答
  •  天命终不由人
    2020-11-28 03:45

    That's because the point of autoload is not to 'require' everything up front (startup penalty). Classes are loaded as they are needed/referenced. In order to do this, you need some way to know where to look for the class. Otherwise, you would have to load every file in the autoload directory in advance to see what classes are declared. It's a tradeoff, but requiring everything in advance (as marbaq suggests) is not autoloading. You can use the autoload command as provided by Ruby, which takes two arguments, the module to load (symbolized, i.e. :Google in your case), and the second argument is the filename, which would be g.rb if lib is in your load path ($:). See the Ruby docs for autoload.

提交回复
热议问题