ruby: irb gives NameError attempting to use mechanize gem (ubuntu) - ruby

On my ubuntu box, irb (ruby) gives a NameError when I try to use the mechanize gem:
$ irb
irb(main):001:0> require 'mechanize'
=> true
irb(main):002:0> Mechanize.new
NameError: uninitialized constant Mechanize
from (irb):2
from :0
gem env shows this:
RubyGems Environment:
- RUBYGEMS VERSION: 1.3.7
- RUBY VERSION: 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]
- INSTALLATION DIRECTORY: /usr/lib/ruby/gems/1.8
- RUBY EXECUTABLE: /usr/bin/ruby1.8
- EXECUTABLE DIRECTORY: /usr/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /usr/lib/ruby/gems/1.8
- /var/lib/gems/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- :gempath => ["/var/lib/gems/1.8", "/usr/lib/ruby/gems/1.8"]
- REMOTE SOURCES:
- http://rubygems.org/
gem list shows it's installed:
$ gem list mech
*** LOCAL GEMS ***
mechanize (1.0.0)
it's in both paths in the filesystem:
$ ls -1 /var/lib/gems/1.8/gems/ |grep mech
mechanize-1.0.0
$ ls -1 /usr/lib/ruby/gems/1.8/gems |grep mech
mechanize-1.0.0
RubyGems is up to date:
$ gem update --system
Updating RubyGems
Nothing to update
# gem list rubygems
*** LOCAL GEMS ***
rubygems-update (1.3.7)
I have an up-to-date ruby:
ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]
Here's what ubuntu gems packages I have (note that I installed rubygems-update and ran update_rubygems):
# dpkg -l rubygems*
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Cfg-files/Unpacked/Failed-cfg/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name Version Description
+++-==================-==================-====================================================
ii rubygems 1.3.5-1ubuntu2 package management framework for Ruby libraries/appl
ii rubygems-doc 1.3.5-1ubuntu2 package management framework for Ruby libraries/appl
ii rubygems1.8 1.3.5-1ubuntu2 package management framework for Ruby libraries/appl

try to create the class using
WWW::Mechanize.new

Related

How to set correct Ruby version in gem environment

Note: This question relates to How can I get bundler to use the Ruby version set by chruby and .ruby-version?.
Using chruby I have Ruby 1.8.7 installed on macOS:
$ ruby -v
ruby 1.8.7 (2013-06-27 patchlevel 374) [i686-darwin17.4.0]
Also, I have used gem update --system 1.8.30 to install a specific version of RubyGems which I am hoping is compatible. This would appear to be installed properly:
$ gem --version
1.8.30
However, my RubyGems environment shows the incorrect version of Ruby (2.5.0):
$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.8.30
- RUBY VERSION: 2.5.0 (2017-12-25 patchlevel 0) [x86_64-darwin17]
- INSTALLATION DIRECTORY: /Users/keithpitty/.gem/ruby/1.8.7
- RUBY EXECUTABLE: /usr/local/opt/ruby/bin/ruby
- EXECUTABLE DIRECTORY: /Users/keithpitty/.gem/ruby/1.8.7/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-17
- GEM PATHS:
- /Users/keithpitty/.gem/ruby/1.8.7
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/
How can I fix the RubyGems environment to use the version of Ruby that chruby has set?
My missing step was installing RubyGems 1.6.2 from source. Following advice elsewhere from Dan Cheail, I did the following:
curl -O https://rubygems.org/rubygems/rubygems-1.6.2.tgz
(and then unzipped it)
cd rubygems-1.6.2
chruby ruby-1.8.7-p374
ruby setup.rb
gem update --system 1.8.25
And then my RubyGems environment was fixed:
$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.8.25
- RUBY VERSION: 1.8.7 (2013-06-27 patchlevel 374) [i686-darwin17.4.0]
- INSTALLATION DIRECTORY: /Users/keithpitty/.gem/ruby/1.8.7
- RUBY EXECUTABLE: /Users/keithpitty/.rubies/ruby-1.8.7-p374/bin/ruby
- EXECUTABLE DIRECTORY: /Users/keithpitty/.gem/ruby/1.8.7/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86-darwin-17
- GEM PATHS:
- /Users/keithpitty/.gem/ruby/1.8.7
- /Users/keithpitty/.rubies/ruby-1.8.7-p374/lib/ruby/gems/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/

Ruby gem fails to load

I've installed a gem from command line:
sudo gem install jalalidate
gem says it's installed:
$ gem list jalalidate
*** LOCAL GEMS ***
jalalidate (0.3.2)
And it can be loaded:
ruby -rubygems -e 'require "jalalidate"' # output is empty, (success?)
Though when I try to include this gem in a Rakefile, rake fails:
# Rakefile:
require "rubygems"
require "bundler/setup"
require "stringex"
require "jalalidate"
# Terminal:
$ rake new_post["title"]
rake aborted!
cannot load such file -- jalalidate
(See full trace by running task with --trace)
Note:
I'm using octopress blogging system, and I'm trying to convert dates generated by Time.now.strftime('%Y-%m-%d %H:%M') to Jalali calender system, (JalaliDate.new(Date.today).strftime('%Y-%m-%d')).
I'm not familiar with Ruby
Update:
$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.8.11
- RUBY VERSION: 1.9.3 (2011-10-30 patchlevel 0) [x86_64-linux]
- INSTALLATION DIRECTORY: /var/lib/gems/1.9.1
- RUBY EXECUTABLE: /usr/bin/ruby1.9.1
- EXECUTABLE DIRECTORY: /usr/local/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /var/lib/gems/1.9.1
- /home/soroush/.gem/ruby/1.9.1
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/
Your Rakefile has require "bundler/setup" before you try to require jalalidate which means you are using Bundler and so you have a Gemfile, which is restricting the gems (and versions) available.
In order to use the gem in your project, add the line
gem "jalalidate"
to the Gemfile.
See this question about why using sudo with gem commands is mostly a bad idea.
I recommend trying to install the gem without sudo: gem install jalalidate

Rails loading wrong version of Ruby

For some reason Rails is loading an older version of Ruby which all is a little odd (since all other indicators say otherwise).
Here's the version outputs from all the usual commands:
# Ruby Version
# $ ruby -v
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
# Rails Version
# $ rails -v
Rails 3.2.6
# Gem Console
# $ gem environment
RubyGems Environment:
- RUBYGEMS VERSION: 1.8.23
- RUBY VERSION: 1.9.3 (2012-04-20 patchlevel 194) [x86_64-linux]
- INSTALLATION DIRECTORY: /usr/local/ruby/1.9.3-p194/lib/ruby/gems/1.9.1
- RUBY EXECUTABLE: /usr/local/ruby/1.9.3-p194/bin/ruby
- EXECUTABLE DIRECTORY: /usr/local/ruby/1.9.3-p194/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /usr/local/ruby/1.9.3-p194/lib/ruby/gems/1.9.1
- /home/vagrant/.gem/ruby/1.9.1
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/
# IRB
# $ irb
irb(main):001:0> RUBY_VERSION
=> "1.9.3"
# Rails Console
# $ rails console
Loading development environment (Rails 3.2.6)
irb(main):002:0> RUBY_VERSION
=> "1.8.7"
Note that only Rails is reporting the wrong version while all others are reporting 1.9.3.
I installed Ruby using the gem environment listed above, so would have expected it to be operating differently...
Any ideas?
If you are using RVM or rbenv both have a project level dotfile (.rbenv, .rvm) that can play havoc with stuff like this. Check there first.

trouble installing rubygem

➜ ~ cloudapp Desktop/test.txt
You need to install cloudapp_api: gem install cloudapp_api
➜ ~ sudo gem install cloudapp_api
Password:
Successfully installed cloudapp_api-0.3.1
1 gem installed
Installing ri documentation for cloudapp_api-0.3.1...
Installing RDoc documentation for cloudapp_api-0.3.1...
➜ ~ cloudapp ~/Desktop/test.txt
You need to install cloudapp_api: gem install cloudapp_api
➜ ~ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.8.5
- RUBY VERSION: 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
- INSTALLATION DIRECTORY: /Library/Ruby/Gems/1.8
- RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
- EXECUTABLE DIRECTORY: /usr/bin
- RUBYGEMS PLATFORMS:
- ruby
- universal-darwin-10
- GEM PATHS:
- /Library/Ruby/Gems/1.8
- /Users/bkern/.gem/ruby/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/
➜ ~
➜ ~ which ruby
/usr/bin/ruby
~ ls -ls /usr/bin/ruby
8 lrwxr-xr-x 1 root wheel 76 Mar 23 10:16 /usr/bin/ruby -> ../../System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby
➜ ~ ruby -v
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
I am not sure how to resolve this. Any help would be appreciated. I am running snow leopard.
Gem.all_load_paths.grep /cloudapp/
NOTE: Gem.all_load_paths is deprecated with no replacement. It will be removed on or after 2011-10-01.
Gem.all_load_paths called from (irb):7.
NOTE: Gem.all_partials is deprecated with no replacement. It will be removed on or after 2011-10-01.
Gem.all_partials called from /Library/Ruby/Site/1.8/rubygems.rb:260.
NoMethodError: undefined method specifications' for "/Library/Ruby/Gems/1.8":String
from /Library/Ruby/Site/1.8/rubygems.rb:425:ineach_load_path'
from /Library/Ruby/Site/1.8/rubygems.rb:423:in each'
from /Library/Ruby/Site/1.8/rubygems.rb:423:ineach_load_path'
from /Library/Ruby/Site/1.8/rubygems.rb:260:in _deprecated_all_load_paths'
from /Library/Ruby/Site/1.8/rubygems.rb:259:ineach'
from /Library/Ruby/Site/1.8/rubygems.rb:259:in _deprecated_all_load_paths'
from /Library/Ruby/Site/1.8/rubygems/deprecate.rb:62:insend'
from /Library/Ruby/Site/1.8/rubygems/deprecate.rb:62:in `all_load_paths'
from (irb):7
The cloudapp binary is very simple..it begins like this:
require 'rubygems'
begin
require 'cloudapp_api'
rescue LoadError
puts "You need to install cloudapp_api: gem install cloudapp_api"
exit!(1)
end
Here's what I would do:
"Make sure you have it plugged in fix": reinstall cloudapp (not the api, just the app).
In a clean irb (start with irb -f). Can you require cloudapp_api there (remember to require rubygems first)?
Check the bang-line (the first line) of the cloudapp binary (use which cloudapp to locate it). Is it running under the correct ruby binary?
These are the first steps of troubleshooting..let's see what you get..

rubygems update broke ruby on OSX

I was having some trouble running some Ruby scripts. While trying to get these scripts to run I tried lots of different things as well as updating rubygems as I thought this may be the problem. Since then I can't get any Ruby scripts to run.
I think that if I can revert rubygems back to it's original version I may be able to run Ruby scripts again, but I don't know how.
$ gem environment
RubyGems Environment:
- RUBYGEMS VERSION: 1.3.5
- RUBY VERSION: 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
- INSTALLATION DIRECTORY: /Library/Ruby/Gems/1.8
- RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
- EXECUTABLE DIRECTORY: /usr/bin
- RUBYGEMS PLATFORMS:
- ruby
- universal-darwin-10
- GEM PATHS:
- /Library/Ruby/Gems/1.8
- /Users/ryan/.gem/ruby/1.8
- /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://gems.rubyforge.org/
$ ruby -v
ruby 1.8.7 (2011-02-18 patchlevel 334) [i686-darwin10]
$ which -a ruby
/opt/local/bin/ruby
/opt/local/bin/ruby
/usr/bin/ruby
/usr/local/bin/ruby
$ which -a gem
/usr/bin/gem
/usr/local/bin/gem
Thank you for any help in advance.
Ryan
$ ruby spidr_start_at.rb
spidr_start_at.rb:1:in `require': no such file to load -- rubygems (LoadError)
from spidr_start_at.rb:1
$ cat spidr_start_at.rb
require 'rubygems'
require 'uri'
require 'spidr'
url = 'http://www.ethicalhack3r.co.uk'
puts "Spidr.start_at()"
puts
Spidr.start_at(url, :depth => 1) do |spider|
spider.every_page do |page|
puts page.url.to_s
end
end
It seems that you have multiple versions of ruby(and gem) installed. It is advised to use rvm when maintaining multiple ruby versions. Or clean up your wrong/old/unneeded installations.
For downgrading Rubygems, you can do the following:
sudo gem update --system VERSION
You obviously have to put a real version there.

Resources