How to test if a module/gem is installed

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

Chris

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.

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

Paul

···

On 21 Oct 2006, at 22:23, snacktime wrote:

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

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.

probably need brackets around trun in the return(true)

Thanks for all the tips guys, was a great help.