Gem loads in irb but not console - ruby

This one is driving me nuts. I can load a gem via irb:
steve#server:/var/www/listings$ irb
irb(main):001:0> Gem.path
=> ["/home/steve/.gem/ruby/1.9.1", "/usr/local/ruby/lib/ruby/gems/1.9.1"]
irb(main):002:0> require 'nokogiri'
=> true
But I can't load it through the rails console:
irb(main):001:0> Gem.path
=> ["/home/steve/.gem/ruby/1.9.1", "/usr/local/ruby/lib/ruby/gems/1.9.1"]
irb(main):002:0> require 'nokogiri'
=> false
The gem (nokogiri) is installed
steve#server:/var/www/listings$ gem which nokogiri
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/nokogiri-1.4.3.1/lib/nokogiri.rb
And bundle agrees
steve#server:/var/www/listings$ bundle show nokogiri
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/nokogiri-1.4.3.1
But, of course, rake spec fails with
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.1/lib/active_support/dependencies.rb:304:in `rescue in depend_on': No such file to load -- Nokogiri (LoadError)
Other environment info:
steve#server:/var/www/listings$ ruby --version
ruby 1.9.2p0 (2010-08-18 revision 29036) [i686-linux]
steve#server:/var/www/listings$ rails --version
Rails 3.0.1
steve#server:/var/www/listings$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.3.7
- RUBY VERSION: 1.9.2 (2010-08-18 patchlevel 0) [i686-linux]
- INSTALLATION DIRECTORY: /usr/local/ruby/lib/ruby/gems/1.9.1
- RUBY EXECUTABLE: /usr/local/ruby/bin/ruby
- EXECUTABLE DIRECTORY: /usr/local/ruby/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86-linux
- GEM PATHS:
- /usr/local/ruby/lib/ruby/gems/1.9.1
- /home/steve/.gem/ruby/1.9.1
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/
Any suggestions??
Edit
By the way, Nokogiri is in the Gemfile and bundle install completes without complaint.
This is a project I'm moving from windows to Ubuntu. On windows it's working fine (oddly enough!) so I'm pretty sure it's an environment thing.

require returns false if that file has already been loaded. Try it out in your irb session by performing the require statement twice in a row. The second one will return false:
irb(main):001:0> require 'nokogiri'
=> true
irb(main):002:0> require 'nokogiri'
=> false
If the file could not be found, require will raise a LoadError instead.
Your exception message (No such file to load -- Nokogiri), makes it seem like something is requiring 'Nokogiri' instead of 'nokogiri', which might be a problem on a case-sensitive operating system.

Make sure that you require it in your Gemfile and do a bundle install.
Edit - Try requiring rubygems, then nokogiri.

Related

Rubygems can't find sinatra directly after install

I can't figure out why rubygems is unable to find sinatra on the require "sinatra" line in the following code. I've tried to write the ruby to ensure that sinatra is installed..
Ruby
$:.push("/home/xxxx/ruby/gems")
require 'rubygems'
begin
gem "sinatra"
rescue LoadError
system("gem install sinatra")
Gem.clear_paths
end
require 'sinatra'
get "/" do
"Hello, world!"
end
Error - on line: require 'sinatra'
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require': no such file to load -- sinatra (LoadError)
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require'
I'm running the script through SSH with the bash command 'ruby app.rb', if that's relevant.
Gem env returns: (where /home/xxxx/ruby/gems is the correct location of the gems)
RubyGems Environment:
- RUBYGEMS VERSION: 1.8.25
- RUBY VERSION: 1.8.7 (2012-06-29 patchlevel 370) [x86_64-linux]
- INSTALLATION DIRECTORY: /home/xxxx/ruby/gems
- RUBY EXECUTABLE: /usr/bin/ruby
- EXECUTABLE DIRECTORY: /home/xxxx/ruby/gems/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-linux
- GEM PATHS:
- /home/xxxx/ruby/gems
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- "gempath" => []
- "gem" => "--remote --gen-rdoc --run-tests"
- "rdoc" => "--inline-source --line-numbers"
- "gemhome" => "/home/xxxx/ruby/gems"
- REMOTE SOURCES:
- http://rubygems.org/
I would recommend using bundler.
First, gem install bundler on both your dev machine and on the server.
Next create a file named Gemfile which contains something like this:
source "https://rubygems.org"
gem "sinatra"
# add a "gem" line for any other gems your application needs
(The command bundle init will create a skeleton Gemfile for you).
Then you do a "bundle install" to install the gems. This will create a "Gemfile.lock" file which gives the exact version of each gem. That file gets checked into your version control along with the Gemfile.
Once that code has been distributed to the server, then "bundle install" on the server to get the exact same gems installed there.
In your application, do not:
require "rubygems"
Modify $:
Install missing gems
On the server where you want to run your application, instead of just ruby app, you will run bundle exec ruby app.

(Ruby) How to import the data from my XML file into SQLite tables

I want to import the data from an XML file into my SQLite tables. What should I do? I really need some help.
Here is my code, I don't know what's wrong with these.
require 'rubygems'
require 'nokogiri'
require 'open-uri'
class AddCoursesData < ActiveRecord::Migration
def up
Course.delete_all
courses = Nokogiri::XML(File.open("courses.xml"))
courses.css('course').each do |node|
children = node.children
Course.create(
acode: node['acode'],
code: node['code'],
sgid: node['sgid'],
name: children.css('name').inner_text,
ects: children.css('ects').inner_text,
year: children.css('year').inner_text,
period: children.css('period').inner_text,
institute: children.css('institute').inner_text,
description: children.css('description').inner_text,
timetable: children.css('timetable').inner_text,
lecture_material: children.css('lecture_material').inner_text,
goals: children.css('goals').inner_text,
participant_count: children.css('participant_count').inner_text,
specialities: children.css('specialities').inner_text,
examination: children.css('examination').inner_text
)
end
end
def down
end
end
and then I input
>rake db:migrate
it says
"rake aborted!
cannot load such file -- nokogiri
D:in 'migrate'
Tasks: TOP=> db:migrate"
I have installed nokogiri. I really don't know how to import these data. So can you help me with this? Thank you very much!!
And here is my rubygems environment
D:\BitNami\rubystack\rubywork\UvAcourses>gem env
RubyGems Environment:
- RUBYGEMS VERSION: 2.0.3
- RUBY VERSION: 1.9.3 (2012-05-25 patchlevel 231) [i386-mingw32]
- INSTALLATION DIRECTORY: D:/BitNami/rubystack/ruby/lib/ruby/gems/1.9.1
- RUBY EXECUTABLE: D:/BitNami/rubystack/ruby/bin/ruby.exe
- EXECUTABLE DIRECTORY: D:/BitNami/rubystack/ruby/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86-mingw32
- GEM PATHS:
- D:/BitNami/rubystack/ruby/lib/ruby/gems/1.9.1
- C:/Users/Lulu/.gem/ruby/1.9.1
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
How old is your Ruby?
Prior to Ruby 1.8.7 we had to start our require list with:
require 'rubygems'
because RubyGems was not part of Ruby's standard library. If we didn't do that we would fail to find all external (non-core and non-standard-lib) gems.
Now days that's not necessary.
Other ways to tell if Nokogiri is installed are to type these at the command-line:
gem list nokogiri
which nokogiri
The first looks through the internal list of installed gems. The second asks Linux or Mac OS OS if it sees the "nokogiri" command, which is installed by the gem.

ruby - require & cannot load such file

I'm new to ruby and I have a problem loading gems.
I've read every topic about this on SO but I couldn't figure out how to make it work :/
I'm on a fresh install of Ruby 1.9.3 and RubyGems 1.8.11
RubyGems Environment:
- RUBYGEMS VERSION: 1.8.11
- RUBY VERSION: 1.9.3 (2011-10-30 patchlevel 0) [i386-mingw32]
- INSTALLATION DIRECTORY: D:/dev/Ruby/lib/ruby/gems/1.9.1
- RUBY EXECUTABLE: D:/dev/Ruby/bin/ruby.exe
- EXECUTABLE DIRECTORY: D:/dev/Ruby/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86-mingw32
- GEM PATHS:
- D:/dev/Ruby/lib/ruby/gems/1.9.1
- D:/aoi/.gem/ruby/1.9.1
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/
I've installed the gem twice, once from command line
gem install soap4r
And second time i've tried to use RubyMine installer, but the result where the same,
when I try to
require 'rubygems'
resuire 'soap'
The output is the same :
LoadError: cannot load such file -- soap
from D:/dev/Ruby/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from D:/dev/Ruby/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from (irb):1
from D:/dev/Ruby/bin/irb:12:in `<main>'
Thank you for your help.
Get this https://github.com/spox/soap4r-spox and patch your ruby with it, its the default soap module for ruby but was pulled out of 1.9 versions
Try to add this to the top of your file:
gem 'soap4r'
require 'soap/wsdlDriver'
Or as an alternative try Savon
Have you checked that you do not run multiple different ruby versions ? Then your gems are not bound to the correct ruby version.
Moreover, I notice something strange :
http://rubygems.org/gems/soap : "This gem has been yanked, but it is still available for download for other gems that may have depended on it"
On the contrary, there seems to be another more interesting gem around : soap4r.
There is a tuto here. I especially noticed that sort of lines :
require "soap/rpc/standaloneserver"
That means "require soap" may not be sufficient in your case.

Ruby gem environment issue - LoadError: no such file to load -- robots

I'm attempting to write a crawler using the anemone gem, which requires the robots gem. For whatever reason, robots absolutely will not include. Here is some of my environment info:
$ gem list -d robots
*** LOCAL GEMS ***
robots (0.10.1)
Author: Kyle Maxwell
Homepage: http://github.com/fizx/robots
Installed at: /usr/local/lib/ruby/gems/1.9.1
Simple robots.txt parser
$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 1.8.10
- RUBY VERSION: 1.9.2 (2011-02-18 patchlevel 180) [x86_64-darwin10.7.0]
- INSTALLATION DIRECTORY: /usr/local/lib/ruby/gems/1.9.1
- RUBY EXECUTABLE: /usr/local/bin/ruby
- EXECUTABLE DIRECTORY: /usr/local/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-10
- GEM PATHS:
- /usr/local/lib/ruby/gems/1.9.1
- /Users/ryan/.gem/ruby/1.9.1
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://rubygems.org/
$ gem which robots
/usr/local/lib/ruby/gems/1.9.1/gems/robots-0.10.1/lib/robots.rb
Any ideas? All other gems load correctly, I've never had this problem before. Note that I am using ruby version 1.9, so rubygems is implicitly required. Adding
require 'rubygems'
...to the front of a script returns false, since the file is already included, and does not help the situation. Any ideas?
EDIT: Posting examples of failing code. Please note that rubygems returning false does not mean rubygems cannot load - rather that it has already been loaded. See this post: http://www.ruby-forum.com/topic/157442
$ irb
irb(main):001:0> require 'rubygems'
=> false
irb(main):002:0> require 'active_record'
=> true
irb(main):003:0> require 'mechanize'
=> true
irb(main):004:0> require 'robots'
LoadError: no such file to load -- robots
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:59:in `require'
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:59:in `rescue in require'
from /usr/local/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
from (irb):4
from /usr/local/bin/irb:12:in `<main>'
irb(main):005:0>
It looks like the gem has been created with the wrong permissions; there's a bug opened for this on github.
Changing the permissions with
sudo chmod a+r /usr/local/lib/ruby/gems/1.9.1/gems/robots-0.10.1/lib/robots.rb
should fix it, but watch out for other permission issues. You might be better with
sudo chmod -R a+r /usr/local/lib/ruby/gems/1.9.1/gems/robots-0.10.1
to recursively make all the files in the gem readable.
The robots.rb file (and some others) is being installed with the permissions -rw-rw----, so anyone using a local install of rvm or similar where gems are installed as the local user won't have been affected by this.

Gem found in irb, not in Ruby

I have some gems installed, and I'm trying to use them in a Ruby app:
require 'rubygems'
require 'mygem'
When I run the app, though, I get this error: <internal:lib/rubygems/custom_require>:29:inrequire': no such file to load -- mygem (LoadError)`
But if I try requiring the gem inside irb (making sure to `require 'rubygems' first), it works fine. What am I supposed to do? I tried googling for this problem, but didn't understand.
Running a which on ruby, gem, and irb shows that they're all in /opt/local/bin/, i.e.,
> which ruby
/opt/local/bin/ruby
> which gem
/opt/local/bin/gem
> which irb
/opt/local/bin/irb
Update to answer the questions posed (yep, irb and ruby are pointing to different folders):
$LOAD_PATH and $: in irb both contain seem to be pointing to ruby 1.8 folders:
/opt/local/lib/ruby/site_ruby/1.8
/opt/local/lib/ruby/site_ruby/1.8/i686-darwin10
/opt/local/lib/ruby/site_ruby
/opt/local/lib/ruby/vendor_ruby/1.8
/opt/local/lib/ruby/vendor_ruby/1.8/i686-darwin10
/opt/local/lib/ruby/vendor_ruby
/opt/local/lib/ruby/1.8
/opt/local/lib/ruby/1.8/i686-darwin10
.
$: in ruby points to ruby 1.9.1 folders:
/usr/local/lib/ruby/site_ruby/1.9.1
/usr/local/lib/ruby/site_ruby/1.9.1/i386-darwin9.8.0
/usr/local/lib/ruby/site_ruby
/usr/local/lib/ruby/vendor_ruby/1.9.1
/usr/local/lib/ruby/vendor_ruby/1.9.1/i386-darwin9.8.0
/usr/local/lib/ruby/vendor_ruby
/usr/local/lib/ruby/1.9.1
/usr/local/lib/ruby/1.9.1/i386-darwin9.8.0
gem env shows
RubyGems Environment:
- RUBYGEMS VERSION: 1.4.1
- RUBY VERSION: 1.8.7 (2010-12-23 patchlevel 330) [i686-darwin10]
- INSTALLATION DIRECTORY: /opt/local/lib/ruby/gems/1.8
- RUBY EXECUTABLE: /opt/local/bin/ruby
- EXECUTABLE DIRECTORY: /opt/local/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86-darwin-10
- GEM PATHS:
- /opt/local/lib/ruby/gems/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- "gempath" => ["/opt/local/lib/ruby/gems/1.8"]
- :sources => ["http://rubygems.org/", "http://gems.github.com", "http://gems.github.com"]
- REMOTE SOURCES:
- http://rubygems.org/
- http://gems.github.com
- http://gems.github.com
Gem.path in irb points to
/Users/grautur/.gem/ruby/1.8
/usr/local/lib/ruby/gems/1.8
Gem.path in ruby points to
/Users/grautur/.gem/ruby/1.9.1
/usr/local/lib/ruby/gems/1.9.1
I'm not sure what's going on. However, the following may help.
In irb, do
require 'rubygems'
require 'mygem'
puts $:
and then, in ruby, do
require 'rubygems'
puts $:
and show us what you get if you haven't worked it out.
Edit: also print out the results of doing gem env on the command line.
Edit 2: See what happens if you type in puts Gem.path after you've required rubygems in both irb and ruby. See thanks to Matt for describing Rubygems
You may try to add gem 'mygem' before the require, but that should not be necessary.
You will have to add gem install mygem in your Gamefile and then run bundle install command. Your application will work correctly after doing this.
I had a similar problem. The solution I found eventually was to setup rvm (ruby version manager) on my system and use it to setup a new ruby environment. it also makes it easy to switch between ruby versions of sets of gems.

Resources