Autoinstall gems

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

say I have:


#!/usr/bin/ruby

require ‘rubygems’
require ‘HOTP’

puts HOTP.new.calculate_hotp(‘12345’, 1, 6)

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

S2 wrote:

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

I think there is a right problem there. Installing gems normally
require super user rights (on modern OS).

Joachim G. wrote:

S2 wrote:

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

I think there is a right problem there. Installing gems normally
require super user rights (on modern OS).

Yes, that is true. It would eventually yield a Permission denied error.
But that is an OS configuration problem, not a rubygems one :slight_smile:

Tom C. wrote:

Well, if I may add my very amateur voice here, it seems to me that one
could certainly trap an error and conditionally issue a system call to
engage the installation process at the super user level. The process
would then have to worked through manually, since there often are one or
more interactions which need to occur to complete the installation of
some gems. This would work fine if the person running the programmer has
access to superuser passwords.

Yes, this is a good alternative.

Don’t know this is the sort of thing you have in mind or not.

I thought that rubygems had his way of doing this automagically, but
after reading your response it looks like this is not the case and it’s
also not really feasible to do easily.

S2 wrote:

error. But that is an OS configuration problem, not a rubygems one :slight_smile:

Well, if I may add my very amateur voice here, it seems to me that one
could certainly trap an error and conditionally issue a system call to
engage the installation process at the super user level. The process
would then have to worked through manually, since there often are one or
more interactions which need to occur to complete the installation of
some gems. This would work fine if the person running the programmer has
access to superuser passwords.

Don’t know this is the sort of thing you have in mind or not.

t.

Tom C., MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< [email protected] >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)

S2 wrote:

error. But that is an OS configuration problem, not a rubygems one :slight_smile:
Yes, this is a good alternative.

Don’t know this is the sort of thing you have in mind or not.

I thought that rubygems had his way of doing this automagically, but
after reading your response it looks like this is not the case and
it’s also not really feasible to do easily.

Well, its apparent that when one manually installs a gem (“sudo install
slideshow”, for example), those already installed are checked to see if
they need to be updated, but normally calling programmatically upon an
uninstalled gem, in any way, produces an error.

t.

Tom C., MS MA, LMHC
Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< [email protected] >> (email)
<< TomCloyd.com >> (website & psychotherapy weblog)
<< sleightmind.wordpress.com >> (mental health issues weblog)
<< directpathdesign.com >> (web site design & consultation)

On Mar 13, 2008, at 01:20 AM, S2 wrote:

require ‘HOTP’

puts HOTP.new.calculate_hotp(‘12345’, 1, 6)

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

No, as the list of files in a gem is only available after installation.

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

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).

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