How to test if a module/gem is installed

S

snacktime

Is there a way to test if a particular module/extension/gem is
installed on the system ruby is running on?

Chris
 
T

Trans

I wrote this extension for that. Not sure how robust it is though.

module Gem

def self.active?(gemname)
@loaded_specs ||= Hash.new
@loaded_specs.key? gemname
end

end

T.
 
P

Paul Lynch

Is there a way to test if a particular module/extension/gem is
installed on the system ruby is running on?

If you know the library name for a require statement, use require and
trap the LoadError? Returns true if the require succeeds.

Paul
 
J

J2M

you can find all the installed gems with a version of this;

def find_my_gem(name)
spec_dir = File.join(Gem.dir, "specifications")
gems = Gem::SourceIndex.from_installed_gems(spec_dir)
gems.each {|path, gem| return true if name == gem.name}
return false
end

this is a bit of a hack together, but gives you the basic idea.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,215
Messages
2,571,113
Members
47,713
Latest member
LeliaB1379

Latest Threads

Top