Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Require all files in sub-directory

I have the following directory tree.

- app.rb
- folder/
  - one/
    - one.rb
  - two/
    - two.rb

I want to be able to load Ruby files in the folder/ directory, even the ones in the sub-directories. How would I do so?

like image 453
Ethan Turkeltaub Avatar asked Dec 24 '10 19:12

Ethan Turkeltaub


People also ask

How do I list all files in subdirectories?

By default, ls lists just one directory. If you name one or more directories on the command line, ls will list each one. The -R (uppercase R) option lists all subdirectories, recursively.

How do I get a list of files in a directory and subfolders in Windows?

Substitute dir /A:D. /B /S > FolderList. txt to produce a list of all folders and all subfolders of the directory.

Can a folder have sub folders?

A subfolder is a folder stored inside another folder. Subfolders help you organize your files more completely. Each subfolder should be used to store files related to each other. For example, you might have one folder for files related to a job search.


2 Answers

Jekyll does something similar with its plugins. Something like this should do the trick:

    Dir[File.join(".", "**/*.rb")].each do |f|
      require f
    end
like image 194
Brian Clapper Avatar answered Oct 19 '22 04:10

Brian Clapper


With less code, but still working on Linux, OS X and Windows:

Dir['./**/*.rb'].each{ |f| require f }

The '.' is needed for Ruby 1.9.2 where the current directory is no longer part of the path.

like image 43
Phrogz Avatar answered Oct 19 '22 05:10

Phrogz