I would like to install a gem (JSON) on the client side, but only if hasn't been installed already (some 1.9 Ruby distros have JSON bundled).
I couldn't find a clue on how to do that from gem help install. And running gem install json on a Windows system with Ruby 1.9 installed (with JSON bundled) results in
ERROR: Error installing json:
The 'json' native gem requires installed build tools.
-- it tries to install it ignoring the fact that the gem is already there.
And I can't do bash tricks like grepping gem list output because the client might be Windows.
So what's the multiplatform way of installing a gem only if it's not present in the system already?
This may work...
begin
require "json"
rescue LoadError
system("gem install json")
end
If you don't want to require "json", you can remove it from $LOAD_PATH.
Or, put as a one liner:
ruby -e 'begin; require "some_gem"; rescue LoadError; system "gem install some_gem"; end'