Autoinstall gems

Is there a way to autoinstall a gem when it is "required" in an .rb file?

Kind of, but not really. The already discussed stuff is important, but also is the fact that for some gems (actually quite a number of gems), the gem name does not match the require. Anyway, enough reasoning, here's solutioning:

-----

#!/usr/bin/env ruby
require 'rubygems'

%w[HOTP].each |gemname|
   begin
     require gemname
   rescue LoadError
     win = Gem.win_platform? rescue RUBY_PLATFORM =~ /(ms|cyg)win|mingw/
     command = "#{'sudo' unless win} gem#{'.bat' if win} install #{gemname}"
     system command
     require gemname
   end
end

puts HOTP.new.calculate_hotp('12345', 1, 6)
-----

Untested, and written in the mail client, anyway, you should get the idea. N.B. Gem.win_platform? is rubygems 0.9.5 (iirc) and above. The platform match is *ok*, but far from ideal.

Another odd problem that can come up from this is to ensure that you're working on the same drive as rubygems. Sadly, some stuff still doesn't work correctly in different places. That is not really a problem on *nix, which has a single root (maybe).

···

On 13 Mar 2008, at 08:20, S2 wrote:

Is there a way to make rubygems autoinstall the HOTP gem if it is not already installed?