Auto installing a gem

I thought it would be clever to require a gem – and then if it wasn’t
found – to install it automatically. I tried this for both a local gem
file and for a remote install. I wasn’t able to get this to work in
either case and am now of the opinion that the Gem library isn’t
designed to support this use case.

Here is one version of my code.

workspace_dir = File.join(File.dirname(FILE), ‘…’, ‘…’)
package_dir = File.join(File.dirname(FILE), ‘…’, ‘packages’)
watir_dir = File.join(workspace_dir, ‘watir’)
gem_file = File.join(package_dir, ‘watir-’+watir_version+’.gem’)
if File.exists?(watir_dir) && use_local_watir
$LOAD_PATH.unshift File.join(workspace_dir, ‘watir’)
else
require ‘rubygems’
begin
require_gem ‘watir’, '>= ’ + watir_version
rescue Gem::LoadError
require ‘rubygems/installer’
specs = Gem::Installer.new(gem_file).install
puts “Gem installed: #{specs}”
end

At this point, i’m just sharing what i’ve learned. If any one has
reached other conclusions, i would be happy to hear about it.

Bret

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs