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
?
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With