Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

RubyGems current load paths

0 views
Skip to first unread message

Intransition

unread,
Nov 20, 2009, 11:40:59 AM11/20/09
to
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.

Eric Hodel

unread,
Nov 23, 2009, 5:38:40 PM11/23/09
to

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.

Intransition

unread,
Nov 23, 2009, 7:09:42 PM11/23/09
to

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?

John Barnette

unread,
Nov 23, 2009, 7:26:46 PM11/23/09
to
On Mon, Nov 23, 2009 at 4:09 PM, Intransition <tran...@gmail.com> 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.

Ryan Davis

unread,
Nov 23, 2009, 7:27:37 PM11/23/09
to

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)


Intransition

unread,
Nov 24, 2009, 2:41:30 AM11/24/09
to

On Nov 23, 7:26 pm, John Barnette <jbarne...@gmail.com> wrote:


> On Mon, Nov 23, 2009 at 4:09 PM, Intransition <transf...@gmail.com> 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.

Eric Hodel

unread,
Nov 24, 2009, 6:06:12 PM11/24/09
to
On Nov 23, 2009, at 23:41, Intransition wrote:
> On Nov 23, 7:26 pm, John Barnette <jbarne...@gmail.com> wrote:
>> On Mon, Nov 23, 2009 at 4:09 PM, Intransition <transf...@gmail.com>
>> 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.

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.


0 new messages