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

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?

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

Try the following syntax (instead of require):
require_gem 'RMagick' , '=1.10'
require_gem 'RMagick' , '>=1.10'
require_gem 'rake', '>=0.7.0', '<0.9.0'

Related

requiring old versions of gems

Since none of the tools I have handy on my Windows laptop can export decent CSV files from SQL Server to save their lives, I figured I'd roll my own in a few lines of Ruby.
Because I'm hitting the version of SQL Server from the turn of the century, I have to use an old version of activerecord-sqlserver-adapter. As far as I can tell I'm doing this correctly, and should be using activerecord ~> 2.2.3 and the 2-3-stable branch of activerecord-sqlserver-adapter, but I am getting an error that complains it is Unable to activate activerecord-sqlserver-adapter-3.2.12, because activerecord-2.2.3 conflicts with activerecord (~> 3.2.0). Here's my code (without anything specifically related to CSVs):
#!/usr/bin/env ruby
gem 'activerecord', "~> 2.2.3"
gem 'activerecord-sqlserver-adapter', github: 'arthrex/activerecord-sqlserver-adapter', :branch => '2-3-stable'
require 'activerecord'
require 'activerecord-sqlserver-adapter'
require 'pry'
ActiveRecord::Base.establish_connection(
:adapter => "sqlserver",
:mode => "odbc",
:username => "c3",
:password => "92641",
:dsn => "Connect3"
)
ActiveRecord::Base.table_name_prefix = 'dbo.'
class Dwnld_Hdr < ActiveRecord::Base
end
pry
Why is it trying to load activerecord (~> 3.2.0) in the first place?
I think in this case you might probably use bundler, because then it will allow you to run the specific gem sets.
Read here and here to get an idea on how to use bundler in a non-Rails project.

require "soap/wsdlDriver" can't load with soap4r gem

I am using ruby version 2.0.0, and Using soap4r (1.5.8).
But i couldn't able to load require "soap/wsdlDriver".
I am trying to use with in rails console,
irb(main):001:0> require 'soap/rpc/driver'
=> false
irb(main):002:0> require 'soap/rpc/driver'
=> false
irb(main):003:0> require "soap/wsdlDriver"
=> false
The standard soap library was removed from Ruby. You will need to find an other solution. Perhaps this gem: https://rubygems.org/gems/soap4r-ruby1.9
Or you refactor you code to use a more up-to-date client. Find a list here: https://www.ruby-toolbox.com/categories/soap

Bunder require yaml/logger

I am using bundler to require all the gems in my project. However, it's not working for yaml/logger.
If I add gem 'yaml' to my gemfile, and run bundle install, I get:
Could not find gem 'yaml (>= 0) ruby' in the gems available on this machine.
But I require it normally just fine. What am I doing wrong?
Thanks
YAML is part of the Ruby Standard Library, and not a Gem.
You do not need to add it to your Gemfile, just require it.
irb(main):001:0> require 'yaml'
=> true
irb(main):002:0> YAML
=> Psych
The same applies for Logger.

How does require rubygems help find rubygem files?

While trying to solve Gem found in irb, not in Ruby , I tried seeing what effect require 'rubygems' had on my own installation:
$ irb
irb(main):001:0> RUBY_VERSION
=> "1.8.7"
irb(main):002:0> $:
["/usr/local/lib/site_ruby/1.8", "/usr/local/lib/site_ruby/1.8/x86_64-linux", "/usr/local/lib/site_ruby", "/usr/lib/ruby/vendor_ruby/1.8", "/usr/lib/ruby/vendor_ruby/1.8/x86_64-linux", "/usr/lib/ruby/vendor_ruby", "/usr/lib/ruby/1.8", "/usr/lib/ruby/1.8/x86_64-linux", "."]
=> nil
irb(main):003:0> require "rubygems" # Hasn't been required already
=> true
irb(main):004:0> require "rubygems" # Will return false, because it's already been required
=> false
irb(main):005:0> $: # Same as before
=> ["/usr/local/lib/site_ruby/1.8", "/usr/local/lib/site_ruby/1.8/x86_64-linux", "/usr/local/lib/site_ruby", "/usr/lib/ruby/vendor_ruby/1.8", "/usr/lib/ruby/vendor_ruby/1.8/x86_64-linux", "/usr/lib/ruby/vendor_ruby", "/usr/lib/ruby/1.8", "/usr/lib/ruby/1.8/x86_64-linux", "."]
If requring rubygems doesn't modify $:, then how does it help find files? Does it monkeypatch require? If so, what variable does it use to find rubygem-installed libraries?
Here's the current version of the relevant source: https://github.com/rubygems/rubygems/blob/02ead548e38ff90923444fa7c0ff9f6a5dbd87b0/lib/rubygems/custom_require.rb. (Edit: here's an earlier version (1.5.2) that more clearly expresses what happens.)
The docs say:
When RubyGems is required, Kernel#require is replaced with our own which
is capable of loading gems on demand.
When you call require 'x', this is what happens:
If the file can be loaded from the existing Ruby loadpath, it
is.
Otherwise, installed gems are searched for a file that matches.
If it's found in gem 'y', that gem is activated (added to the
loadpath).
The normal require functionality of returning false if
that file has already been loaded is preserved.
It does this by opening up module Kernel and aliasing the original require with alias gem_original_require require, then redefining require to first call the original version, and look at the gems if that doesn't work.
So the load path is only changed when you require a gem:
ruby-1.8.7-p330 :002 > $:.length
=> 9
ruby-1.8.7-p330 :003 > require 'rubygems'
=> true
ruby-1.8.7-p330 :004 > $:.length
=> 9
ruby-1.8.7-p330 :005 > require 'haml'
=> true
ruby-1.8.7-p330 :006 > $:.length
=> 10

What module(s) do I need to include to get 5.seconds to work in Ruby?

Because apparently require 'date' doesn't include the method hours or seconds etc:
undefined method `hours' for 5:Fixnum (NoMethodError)
Am I missing something? Is 5.seconds only something you can do in Rails? If so, what is the require statement I need to get this to work in a ruby script?
Old question, but for the googlers like me:
require 'active_support/time'
For gem version 3.2.11, anyway.
The following works for me
irb
>> require 'active_support'
=> true
>> 5.hours
=> 18000 seconds
Depending on your environment and rails version you may need to require 'rubygems' this should be done before the require 'active_support' line.
You may also have to require 'activesupport' instead of active_support if you have an older version of rails.
ActiveSupport::CoreExtensions::Numeric::Time maybe

Resources