4

In a gemspec, I can specify the require_paths, which represent paths I want to be able to require from at runtime. These get put into the $LOAD_PATH by RubyGems.

My question is: is there a way I can determine what these paths are at runtime? Can I examine elements of $LOAD_PATH and know which ones were added just for my gem?

Update: Ultimately, I would like to dynamically load ruby files from inside the gem, e.g.

load_from 'foo/bar'

And have that find $MY_GEMS_LIB_DIR/foo/bar/*.rb. I can certainly go through the entire $LOAD_PATH looking for foo/bar, but I'd rather restrict it just to the gem.

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
davetron5000
  • 24,123
  • 11
  • 70
  • 98

4 Answers4

1

I don't know if I understood your needing (my english is bad :-/ ); anyway, if the problem is to determine the directories that will be loaded when you require a gem 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

So 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

Trying to load Thor::CoreExt :

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

This works on my machine with ruby 1.9.3 and gem 1.8.21 .

mdesantis
  • 8,257
  • 4
  • 31
  • 63
1

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.

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
0

Looks like Gem.find_files may help you.

Nash Bridges
  • 2,350
  • 14
  • 19
  • That's not quite it; it seems to search within the installed gems. Perhaps what I want is not possible. I was hoping I could intercept the load paths added by the gem in which my code is currently running. – davetron5000 Apr 10 '12 at 03:30
  • @davetron5000 BTW gem's load path can be added to `$LOAD_PATH` by Bundler. In that case Rubygems activation mechanism won't be used at all. – Nash Bridges Apr 10 '12 at 06:19
  • Would that happen for a command-line app installed via RubyGems? – davetron5000 Apr 10 '12 at 12:22
  • @davetron5000 Ah, I was inattentive to your previous comment about command-line app. Of course no, unless you `require 'bundler/setup'` yourself. – Nash Bridges Apr 10 '12 at 16:53
0

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

Femaref
  • 60,705
  • 7
  • 138
  • 176
  • That is how one access the load path. I know how to do that. I want to see what part of the load path was modified by RubyGems specific to my app – davetron5000 Mar 26 '12 at 00:21