Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I require a specific version of a ruby gem?

Specifically, the ruby-oci8 gem. I have both 1.0.7 and 2.0.4 installed. I want 1.0.7.

I can just require oci8, but I don't get the version I want.

irb(main):001:0> require 'oci8' => true irb(main):002:0> OCI8::VERSION => "2.0.4" 

I can require using the full path to the file, which works, but is not going to be portable:

irb(main):001:0> require 'C:\Ruby\lib\ruby\gems\1.8\gems\ruby-oci8-1.0.7-x86-mswin32-60\lib\oci8' => true irb(main):002:0> OCI8::VERSION => "1.0.7" 

I can use the gem command to ask for the version I want, but it doesn't appear to actually load the library:

irb(main):001:0> gem 'ruby-oci8', :lib=>'oci8', :version=>'=1.0.7' => true irb(main):002:0> OCI8::VERSION NameError: uninitialized constant OCI8     from (irb):2 

I would definitely favor this last approach if would load the library, rather than just confirming that it's present on my system. What am I missing?

like image 563
KenB Avatar asked Apr 22 '10 19:04

KenB


People also ask

How do I install a specific version of a Ruby gem?

Use `gem install -v` You may already be familiar with gem install , but if you add the -v flag, you can specify the version of the gem to install. Using -v you can specify an exact version or use version comparators.

How do I specify a gem version?

There are several ways to specify gem versions: Use a specific version: gem "name-of-gem", "1.0" . You can find specific versions on Rubygems.org (provided that's the source you”re using) by searching for your gem and looking at the “Versions” listed. Use a version operator: gem "name-of-gem", ">1.0" .

How do I install a specific version of Ruby on Linux?

To install and use a different version of Ruby, run the rbenv commands with a different version number, as in rbenv install 2.3. 0 followed by rbenv global 2.3. 0 . You now have at least one version of Ruby installed and have set your default Ruby version.


1 Answers

My problem was twofold:

1) confusing gem command syntax with that used in config.gem lines in a rails environment.rb configuration file.

2) failing to issue a require command after the gem command.

Proper usage in a script is:

gem 'ruby-oci8', '=1.0.7' require 'oci8'           # example is confusing; file required (oci8.rb) is not                           # same name as gem, as is frequently the case 

Proper usage in a rails 2.3.x environment.rb file is:

config.gem "ruby-oci8", :version=>'1.0.7' 

Thanks to the folks at http://www.ruby-forum.com/topic/109100

like image 164
KenB Avatar answered Sep 29 '22 00:09

KenB