Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to require all files in a directory, in a gem?

Tags:

ruby

rubygems

I know that I can require all files in a directory using

Dir['path/to/files/**/*.rb'].each { |file| require file }

However, I'm trying to do that for a gem setup file, like so:

class MyGem
  module NS; end
end

Dir['lib/my_gem/files/**/*.rb'].each { |file| require file }

(where all the files in lib/my_gem/files are namespaced under MyGem::NS). With a directory structure that looks like this:

lib                              
|-- my_gem                        
|   |-- files              
|   |   |-- file1.rb       
|   |   |-- file2.rb         
|   |   |-- file3.rb         
|   |   `-- ... (many more)      
`-- my_gem.rb                     
my_gem.gemspec

And, well, the Dir command doesn't seem to work when I require my gem in another file, since it seems to look for the file paths at run time in my project root (of whatever project I've required my gem from).

How can I require all the files in lib/my_gem/files in lib/my_gem.rb?

like image 308
neezer Avatar asked Jul 23 '12 17:07

neezer


1 Answers

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 }
like image 115
Joshua Cheek Avatar answered Oct 19 '22 12:10

Joshua Cheek