shabunc
shabunc

Reputation: 24741

How to modify GEM_PATH without explicitly modifying environment variable

I wonder what is the best way to run ruby with defined path to gems without setting GEM_PATH explicitly.

So, basically I wonder whether it possible to do this:

ruby --some-magic-gem-path my/gems script.rb

instead of this:

export GEM_PATH=my/gems
ruby script.rb
unset GEM_PATH

My first thought was: "A-ha, I just can do something like ruby -r rubygems -e "Gem.path << 'my/gems'" script.rb" but then I realized that script.rb in this case won't run at all.

Upvotes: 2

Views: 618

Answers (1)

vgoff
vgoff

Reputation: 11313

Perhaps use the -e flag in Ruby to unshift your folder where gems should be searched for.

$ ruby -e 'Gem.path.unshift("~/bin")' -e "p Gem.path"
["~/bin", "/home/vgoff/.rvm/gems/ruby-2.0.0-p247", "/home/vgoff/.rvm/gems/ruby-2.0.0-p247@global"]

I think though that this still won't allow you to run the program.

So, it may be better to include the file that has your custom need in it using -r custom_gem_path.rb rather than invoking the -e flag.

ruby -r./custom_gem_path.rb test.rb

This gives me the same output as above, given that what in the first -e argument is stored in ./custom_gem_path.rb.

Upvotes: 1

Related Questions