There's a subtle gotcha to not stripping the extension. If some other part of the code calls require 'foo' then ruby will load the same file again, which can lead to spurious errors. I added my own answer which explains that and shows how to strip the extension.
–
Pete HodgsonFeb 9 '10 at 18:40

This might be obvious, but its worth noting that dropping the .rb will also require any non-.rb files in the dir, which might not be desired.
–
louismJun 18 '12 at 2:53

4

@PeteHodgson's suggestion is inaccurate. Ruby's require is not confused by the presence or absence of the .rb extension. Tested on MRI 1.8.7-p374, 2.1.5 and 2.2.0 tested. This urban legend comes from Rails, where "clever" autoloading exhibited the behaviour he describes in older versions (and may still exhibit it).
–
sheldonhFeb 25 at 7:32

@clacke I added your links to the original answer, thanks for the comment.
–
Fábio BatistaAug 14 '11 at 21:01

I needed to include all of my ActiveRecord models, the require_all gem figured out all of the dependencies and required them perfectly. Thanks!
–
panupanDec 7 '11 at 1:39

2

@panupan Just be aware that require_all's cyclic dependency resolution works around a problem in your source code: you have Ruby source files that do not require their dependencies. This shuts the door on scalpel loading, committing you to all-or-nothing loading. That's not a problem in small libraries, but it's a decision you should be making consciously.
–
sheldonhFeb 25 at 7:36

If you don't strip the extension then you may end up requiring the same file twice (ruby won't realize that "foo" and "foo.rb" are the same file). Requiring the same file twice can lead to spurious warnings (e.g. "warning: already initialized constant").

Is this really the case? Documentation says: A feature will not be loaded if its name already appears in $". The file name is converted to an absolute path, so "require 'a'; require './a'" will not load a.rb twice. ruby-doc.org/core/classes/Kernel.html#M001418
–
DerekJan 29 '11 at 17:47

No. Ruby's require is not confused by the presence or absence of the .rb extension. Tested on MRI 1.8.7-p374, 2.1.5 and 2.2.0. This urban legend comes from Rails, where "clever" autoloading exhibited the behaviour described in older versions (and may still exhibit it).
–
sheldonhFeb 25 at 7:32

The best way is to add the directory to the load path and then require the basename of each file. This is because you want to avoid accidentally requiring the same file twice -- often not the intended behavior. Whether a file will be loaded or not is dependent on whether require has seen the path passed to it before. For example, this simple irb session shows that you can mistakenly require and load the same file twice.

Note that the first two lines return true meaning the same file was loaded both times. When paths are used, even if the paths point to the same location, require doesn't know that the file was already required.

Here instead, we add a directory to the load path and then require the basename of each *.rb file within.

If you don't care about the file being required more than once, or your intention is just to load the contents of the file, perhaps load should be used instead of require. Use load in this case, because it better expresses what you're trying to accomplish. For example:

Since Rails.root is a Pathname instance, you can do this in any Ruby environment, not just Rails (N.B. Rails.root.join('lib/ext/*.rb') reads a little nicer)
–
DMKESep 25 '14 at 22:00

Thanks for the recommendation; I edited to include your comment.
–
dankohnSep 26 '14 at 2:24

Using a forward slash (/) for sub-directories under Rails.root, for e.g. Rails.root.join('/lib') doesn't generate correct path. I found this one to work correctly: Dir[Rails.root.join('lib', 'ext', '*.rb')].each { |file| require file }
–
Jiggneshh GohelJul 17 at 12:46