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

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

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

However, I am trying to do this for a gem installation file, for example:

 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 named 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 need my gem in another file, as it seems to be looking for file paths at runtime in my project root directory (from any project that I have mine gemstone).

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

+8
ruby rubygems
source share
2 answers

Rubygems has a tool called find_files that allows you to search based on something in the download path:

 Gem.find_files("my_gem/files/**/*.rb").each { |path| require path } 
+12
source share

You can perform a recursive search with find, and then run a list of answers and include them line by line.

Something like

 `find #{root_path}/lib/my_gem/files/**/*.rb`.split("\n").each { |p| require p } 
-one
source share

All Articles