Can I determine which RubyGems paths are added to the boot path for my command line application to work?

In gemspec, I can specify require_paths that represent the paths that I want to get from at runtime. They fall into $LOAD_PATH RubyGems.

My question is: is there a way to determine that these paths are at runtime? Can I look at the $LOAD_PATH elements and find out which ones were added only for my gem?

Refresh . Ultimately, I would like to dynamically load ruby ​​files from within the gem, for example.

 load_from 'foo/bar' 

And find $MY_GEMS_LIB_DIR/foo/bar/*.rb . I can, of course, go through the entire $LOAD_PATH looking for foo/bar , but I would rather limit it to just a stone.

+4
source share
4 answers

I do not know if I understood your need (my English is bad: - /); In any case, if the problem is to determine the directories that will be loaded when you need the stone, you can use Gem::Specification.lib_dirs_glob :

 Gem::Specification.find_by_name('irbtools').lib_dirs_glob #=> "/home/my_user/.rvm/gems/ruby-1.9.3-p125/gems/irbtools-1.2.2/lib" Gem::Specification.find_by_name('xyz').lib_dirs_glob # raises a Gem::LoadError 

Thus, a possible implementation of load_from could be:

 def load_from(gem_name, path) path_to_load = File.join(Gem::Specification.find_by_name(gem_name).lib_dirs_glob, path) Dir.glob(path_to_load).each(&method(:load)) end 

Attempting to download Thor::CoreExt :

 Thor::CoreExt #=> NameError: uninitialized constant Thor load_from 'thor', 'thor/core_ext/*.rb' Thor::CoreExt #=> Thor::CoreExt 

It works on my machine with ruby ​​1.9.3 and gem 1.8.21.

+1
source

If I understand you correctly, this should do (Ruby 1.9.3):

 before = $LOAD_PATH.dup require 'the_gem' added_paths = $LOAD_PATH - before 

Of course, this will include the paths added by the dependencies.

+1
source

You can use global $: in irb. There is also a gem which command that gives you the path to the library, but I'm not sure if this includes what you want.

0
source

Looks like Gem.find_files can help you.

0
source

Source: https://habr.com/ru/post/1403368/


All Articles