20

I am working on an API that needs to load all of the .rb files in its current directory and all subdirectories. Currently, I am entering a new require statement for each file that I add but I would like to make it where I only have to place the file in one of the subdirectories and have it automatically added.

Is there a standard command to do this?

2
  • 1
    Just a note: REQUIRING a set of possibly unknown files is dangerous and also can lead to errors. Let's say I put a file called die.rb with an exit statement in it (sorry, no actual code, I've never used Ruby), as soon as it is loaded, the program ends. Or what if the user deletes a file? The functions provided by that file would not be loaded. If you just want to disregard the security issue, including the files and checking to make sure they were loaded (by calling some sort of check function), might work, but again, no Ruby experience.
    – HalfBrian
    Dec 4, 2009 at 21:37
  • Why would you put die.rb in your project? If the project is already old and has collected junk I would agree this is dangerous. For a newish project, however, it's a sensible move that makes it hard for somebody to accidentally add die.rb to the codebase (it would fail on load/bugs made clear sooner rather than later)
    – Adamantish
    Apr 18, 2019 at 10:06

6 Answers 6

45

In this case its loading all the files under the lib directory:

Dir["#{File.dirname(__FILE__)}/lib/**/*.rb"].each { |f| load(f) }
5
  • 1
    why do you use load and not require?
    – johannes
    Dec 6, 2009 at 14:12
  • You can use both, but require is usually passed a library name, with no extension, rather than a file name. Dec 6, 2009 at 14:58
  • 2
    but require, doesn't mind if you pass in the extension, so in my opinion it is preferable
    – johannes
    Dec 6, 2009 at 20:28
  • 15
    If you use require, the file will be treated as a source file and loaded as a library. Using load causes the file to be run as a script and will prevent any local variables in the loaded file from being imported into the current environment. Ruby will also let you re-import a file with load but not with require. Either syntax can be used here, just make sure it is doing what you think it is doing.
    – bta
    Dec 10, 2009 at 16:44
  • 1
    "Libraries" are just files afterall. When you have require 'bundler', Ruby finds the file bundler.rb in the lib directory of the bundler gem and runs the code in that file.
    – Nathan
    Mar 27, 2014 at 23:06
6
require "find"

Find.find(folder) do |file|
  next if File.extname(file) != ".rb"
  puts "loading #{file}"
  load(file)
end

This will recursively load each .rb file.

4

like Miguel Fonseca said, but in ruby >= 2 you can do :

    Dir[File.expand_path "lib/**/*.rb"].each{|f| require_relative(f)}
2

I use the gem require_all all the time, and it gets the job done with the following pattern in your requires:

require 'require_all'
require_all './lib/exceptions/'
0
def rLoad(dir)
    Dir.entries(dir).each {|f|
        next if f=='.' or f=='..'
        if File.directory?(f)
            rInclude(f)
        else
            load(f) if File.fnmatch('*.rb', f)
        end
    }
end

This should recursively load all .rb files in the directory specified by dir. For example, rLoad Dir.pwd would work on the current working directory.

Be careful doing this, though. This does a depth-first search and if there are any conflicting definitions in your Ruby scripts, they may be resolved in some non-obvious manner (alphabetical by folder/file name I believe).

4
  • Why aren't you matching directly against f in your load call?
    – Geo
    Dec 5, 2009 at 11:13
  • If you are referring to the statement File.fnmatch('*.rb', f), the statement returns true if the filename f matches the pattern *.rb. This ensures that we are only load-ing ruby scripts and not any other files that might happen to be in the folder. Note that you may have to add another condition if you also have some scripts that use the .rbw extension.
    – bta
    Dec 7, 2009 at 20:14
  • Yes. But you could have used load(f) if f =~/\.rb/. I was referring to why you're using fnmatch over the builtin regex support.
    – Geo
    Dec 8, 2009 at 16:42
  • Ah, I see what you mean now. When I wrote the above code I happened to be working on something else that was using the fnmatch method so that line was pretty much a copy/paste. I suppose ~/\.rb$/ regex would work as well (added the $ to prevent matching files like "script.rb.bak").
    – bta
    Dec 10, 2009 at 16:33
0

You should have a look at this gem. It is quite small so you can actually re-use the code instead of installing the whole gem.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.