How to require some lib files from anywhere

47,949

Solution 1

You can autoinclude everything under the lib folderand avoid these problems:

Type this your file in config/application.rb

config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]

Solution 2

If you want to require only a specific file then, do something relative to Rails root like this

for example: --

lib/plan.rb

module Plan
 ...some code...
end

and if you want to require it only in some model, say app/models/user.rb

do in user model

require "#{Rails.root}/lib/plan"

class User < ActiveRecord::Base
  include Plan
end

if you want it to be available everywhere

one solution is given by @VecchiaSpugna

or you can create a ruby file in config/initializers folder

and require all file over there one by one

OR

try this

require '../../my_module/my_file'

instead of 

require '../../my_module/my_file.rb'

You don't need to specify extension for a file in require.

Solution 3

I think there are two solutions.

1) Add the lib path to the search path. In ruby:

$:.unshift('../../my_module/lib')

Then you can require 'my_module.rb'

I think Vecchia Spugna answer is the rails-version of my ruby-answer. (I'm not familiar with rails).

2) Another solution:

In your lib/my_module.rb you require my_file. This file is located relative to your my_module.rb? Then use require_relative:

require_relative './my_module/my_file'

Solution 4

Just chiming in because it took me forever to figure this out because very little solutions worked.

• I had to use plain old require. I put it in the config/application.rb file.

patching_file_path = File.expand_path("./lib", Dir.pwd) Dir[patching_file_path+'/*.rb'].each {|file| require file }

• I also put a temporary puts "I'm Working! in the file I'm trying to require so I can check the console to see if it's actually loading.

• Also, if you're using spring loader, before you start your console you should do bin/spring stop in your terminal before you start your rails console. Otherwise, it won't load new files.

Share:
47,949

Related videos on Youtube

Zoz
Author by

Zoz

Updated on July 02, 2020

Comments

  • Zoz
    Zoz almost 4 years

    I'll explain my situation.

    Here is my file tree in my rails application :

    lib/my_module.rb

    require 'my_module/my_file'
    
    module My_module
    
    end
    

    lib/my_module/my_file.rb

    class Tweetag::Collector
       (...)
    end
    

    I've made a ruby script that I've put in config/jobs/

    I really don't understand how I am supposed to require the file my_file.rb in this file.

    require '../../my_module/my_file.rb'
    

    It gives me `require': cannot load such file

    Same error with just require 'my_module' which is what I do in my controllers...

    Someone here to explain to me ? Thanks a lot

  • Zoz
    Zoz almost 11 years
    I did it, but still : `<main>': uninitialized constant MyModule (NameError)
  • Vecchia Spugna
    Vecchia Spugna almost 11 years
    now include your module where you need it, with include My_module
  • Vecchia Spugna
    Vecchia Spugna almost 11 years
    you declared My_module but you are using MyModule. Fix this inconsistency. However the convention is to use MyModule form for the modules you define
  • Zoz
    Zoz almost 11 years
    Well actually you can replace all the "my_module" by "Tweetag" and all the "my_file" by "Collector". I tried to be generic in my explanations but I failed. Anyways, when I do "include Tweetag", it doesn't work.
  • Vecchia Spugna
    Vecchia Spugna almost 11 years
    have you restarted your application server, so that the config file is read?
  • Zoz
    Zoz almost 11 years
    Well actually I just understood that I cannot do something specific to rails as you just said. I juste would like to run the command line "ruby my_ruby_script.rb" and require my lib module in it.
  • tvw
    tvw about 7 years
    1) is the way to go, since the job in config/jobs seems not to be started from within his rails application. Therefore he must setup the load path himself as you describe it rather than expecting, that a not used rails environment will do this for him.
  • Fábio Araújo
    Fábio Araújo over 5 years
    On production server with Rails 5.2.2 you might need to use config.eager_load_paths += %W(#{config.root}/lib)
  • Jason Swett
    Jason Swett almost 5 years
    Wouldn't it be more idiomatic to do require Rails.root.join('lib', 'plan')?
  • Jarno Lamberg
    Jarno Lamberg about 3 years
    I was also stuck with this for a while. Stopping Spring did the trick for me. Cheers!