# RubyGems current load paths

**URL:** <https://www.ruby-forum.com/t/rubygems-current-load-paths/179263>\
**Category:** Ruby\
**Created:** [November 20, 2009, 5:41pm UTC](https://www.ruby-forum.com/t/rubygems-current-load-paths/179263 "2009-11-20T17:41:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [November 20, 2009, 5:41pm UTC](https://www.ruby-forum.com/t/rubygems-current-load-paths/179263/1 "2009-11-20T17:41:46Z")

</div>

Hi–

I need to get a list of the “current” gem load paths. I learned that I  
can get a list of all the latest load paths via:

Gem.latest\_load\_paths

which is great. But lets say I specify an older version of a lib, eg:

gem “RubyInline”, “= 3.7.0”

Gem.latest\_load\_paths doesn’t change. How can I get a list of latest  
load paths but adjusted to reflect any specified gem versions?

Thanks.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [November 23, 2009, 11:40pm UTC](https://www.ruby-forum.com/t/rubygems-current-load-paths/179263/2 "2009-11-23T23:40:10Z")

</div>

On Nov 20, 2009, at 08:40, Intransition wrote:

> load paths but adjusted to reflect any specified gem versions?  
> Gem.latest\_load\_paths works for all installed gems, even if they’re  
> not activated. You’ll need to use Gem::Specification#full\_gem\_path  
> and Gem::Specification#require\_path.

Needing to know where the gem lives is a sign that you’re doing  
something wrong though.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [November 24, 2009, 1:15am UTC](https://www.ruby-forum.com/t/rubygems-current-load-paths/179263/3 "2009-11-24T01:15:33Z")

</div>

On Nov 23, 5:38 pm, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Gem.latest\_load\_paths doesn’t change. How can I get a list of latest  
> > load paths but adjusted to reflect any specified gem versions?
> 
> Gem.latest\_load\_paths works for all installed gems, even if they’re  
> not activated. You’ll need to use Gem::Specification#full\_gem\_path  
> and Gem::Specification#require\_path.
> 
> Needing to know where the gem lives is a sign that you’re doing  
> something wrong though.

I’m doing a little meta-programming in this case – I’ve created a  
function to find “plugins”. So I need to search through the $LOAD\_PATH  
and Gems. But I’m all ears if there is another way to do it. Here’s  
the code:

# = Plugin Handler

# 

# Find plugins across various library managers.

# 

module Plugin

```
extend self

DIRECTORY = 'plugin'

# Find plugins, searching through standard $LOAD_PATH,
# Roll Libraries and RubyGems.
#
# Provide a +match+ file glob to find plugins.
#
# Plugins.find('syckle/*')
#
def find(match)
  plugins = []

  # Standard $LOAD_PATH
  $LOAD_PATH.uniq.each do |path|
    list = Dir.glob(File.join(path, DIRECTORY, match))
    #dirs = dirs.select{ |d| File.directory?(d) }
    list = list.map{ |d| d.chomp('/') }
    plugins.concat(list)
  end

  # ROLL (load latest or current versions only)
  if defined?(::Roll)
    ::Roll::Library.ledger.each do |name, lib|
      lib = lib.sort.first if Array===lib
      lib.load_path.each do |path|
        find = File.join(lib.location, path, DIRECTORY, match)
        list = Dir.glob(find)
        list = list.map{ |d| d.chomp('/') }
        plugins.concat(list)
      end
    end
  end

  # RubyGems (load latest versions only)
  # TODO: need current versions
  if defined?(::Gem)
    Gem.latest_load_paths do |path|
      list = Dir.glob(File.join(path, DIRECTORY, match))
      list = list.map{ |d| d.chomp('/') }
      plugins.concat(list)
    end
  end

  plugins
end

# Shortcut for #find.
#
# Plugins['syckle/*']
#
alias_method :[], :find

```

end

Also, I wonder how Ruby 1.9 might effect this. Are the latest gem  
paths added to the $LOAD\_PATH by default in 1.9?

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [November 24, 2009, 1:27am UTC](https://www.ruby-forum.com/t/rubygems-current-load-paths/179263/4 "2009-11-24T01:27:16Z")

</div>

On Mon, Nov 23, 2009 at 4:09 PM, Intransition [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I’m doing a little meta-programming in this case – I’ve created a  
> function to find “plugins”. So I need to search through the $LOAD\_PATH  
> and Gems. But I’m all ears if there is another way to do it.

Have you looked at Gem.find\_files? It was written to solve this problem.

~ j.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [November 24, 2009, 1:28am UTC](https://www.ruby-forum.com/t/rubygems-current-load-paths/179263/5 "2009-11-24T01:28:26Z")

</div>

On Nov 23, 2009, at 16:09 , Intransition wrote:

> I’m doing a little meta-programming in this case – I’ve created a  
> function to find “plugins”. So I need to search through the $LOAD\_PATH  
> and Gems. But I’m all ears if there is another way to do it. Here’s  
> the code:

Gem.find\_files(glob)

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [November 25, 2009, 12:08am UTC](https://www.ruby-forum.com/t/rubygems-current-load-paths/179263/6 "2009-11-25T00:08:04Z")

</div>

On Nov 23, 2009, at 23:41, Intransition wrote:

> Yes, I looked at that. But there is a problem. The docs say,
> 
> “Note that find\_files will return all files even if they are from  
> different versions of the same gem.”
> 
> I want only the active or latest gem versions.

It’s an encouragement to make your plugin files as light as possible,  
such as requiring an additional file or calling some very stable API.

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [November 24, 2009, 8:42am UTC](https://www.ruby-forum.com/t/rubygems-current-load-paths/179263/7 "2009-11-24T08:42:08Z")

</div>

On Nov 23, 7:26 pm, John B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Mon, Nov 23, 2009 at 4:09 PM, Intransition [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I’m doing a little meta-programming in this case – I’ve created a  
> > function to find “plugins”. So I need to search through the $LOAD\_PATH  
> > and Gems. But I’m all ears if there is another way to do it.
> 
> Have you looked at Gem.find\_files? It was written to solve this problem.

Yes, I looked at that. But there is a problem. The docs say,

“Note that find\_files will return all files even if they are from  
different versions of the same gem.”

I want only the active or latest gem versions.

However, short of a better solution, I guess I can look at the code  
behind that method for some ideas.
