Best way to require all files from a directory in ruby?
How about:
Dir["/path/to/directory/*.rb"].each {|file| require file }
Cleanest/One-liner way to require all files in directory in Ruby?
project_root = File.dirname(File.absolute_path(__FILE__))
Dir.glob(project_root + '/helpers/*') {|file| require file}
Or to golf it a bit more:
Dir.glob(project_root + '/helpers/*', &method(:require))
Require all files in sub-directory
Jekyll does something similar with its plugins. Something like this should do the trick:
Dir[File.join(".", "**/*.rb")].each do |f|
require f
end
How to 'require' all files from a directory and its sub-directories (Ruby)?
To require all the files, there is a special gem which does an amazing job. It's require_all
but don't install the lastest one.
Install
gem install require_all -v 1.5.0
It provides two beautiful methods require_all()
and require_rel()
! For require_rel, paths are relative to the caller rather than the current working directory
Is it possible to recursively require all files in a directory in Ruby?
In this case its loading all the files under the lib directory:
Dir["#{File.dirname(__FILE__)}/lib/**/*.rb"].each { |f| load(f) }
How to require all files in a directory, in a gem?
Rubygems has a facility called find_files that will allow you to search based anything in the load path:
Gem.find_files("my_gem/files/**/*.rb").each { |path| require path }
What's a good way to require a whole directory tree in Rails?
autoload_paths
and friends work only if the given files, or files in the given directories, are named according to the rails naming conventiion.
e.g. if a file some_class.rb
is given to autoload_paths
, it expcects the file to declare a class SomeClass
, and sets up some magic to make any reference to SomeClass
load that file on-the-fly.
So if you want to have it only load each of your files as they are needed, then you will have to name your files accordingly, and have one file per class.
If you are happy to load all of the files in a directory tree when rails starts, you can do this:
Dir.glob("/path/to/my/lib/**/*.rb").each { |f| require f }
The above will read every .rb
file in /path/to/my/lib
, and any directories under it.
Get names of all files from a folder with Ruby
You also have the shortcut option of
Dir["/path/to/search/*"]
and if you want to find all Ruby files in any folder or sub-folder:
Dir["/path/to/search/**/*.rb"]
Related Topics
How to Parse Json With Ruby on Rails
Connecting Rails 3.1 With Multiple Databases
How to Create Multiple Submit Buttons For the Same Form in Rails
Rhc Setup Gives Error 'No Such File Dl/Import'
How to Extract Url Parameters from a Url With Ruby or Rails
Saving Enum from Select in Rails 4.1
Failed to Build Gem Native Extension (Installing Compass)
Measure the Distance Between Two Strings With Ruby
Bundle Install Fails With Ssl Certificate Verification Error
How to Find and Return a Duplicate Value in Array
In Ruby on Rails, How to Format a Date With the "Th" Suffix, as In, "Sun Oct 5Th"
How to Update Ruby Version 2.0.0 to the Latest Version in MAC Osx Yosemite
How to Find an Item in Array Which Has the Most Occurrences