Why can't I load Nokogiri? - ruby

I installed Nokogiri without any issues by running:
$ sudo gem install nokogiri
Building native extensions. This could take a while...
Successfully installed nokogiri-1.5.9
1 gem installed
Installing ri documentation for nokogiri-1.5.9...
Installing RDoc documentation for nokogiri-1.5.9...
When I run nokogiri.rb:
#!/usr/bin/ruby -w
require 'nokogiri'
puts "Current directory is: #{ Dir.pwd }"
Dir.chdir("/home/askar/xml_files1") do |dirname|
puts "Now in: #{ Dir.pwd }"
xml_files = Dir.glob("ShipmentRequest*.xml")
if xml_files.empty?
puts "#{ dirname } is empty."
else
xml_files.each do |file|
doc = Nokogiri::XML(open(file))
# ... do something with the doc ...
end
end
end
I got the error:
$ ruby nokogiri.rb
/home/askar/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- nokogiri (LoadError)
from /home/askar/.rvm/rubies/ruby-1.9.3-p429/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
I'm using Ruby 1.9.3, but for some reason it refers to Ruby 1.9.1. Can this be the cause?

If you observe closely, the path starts with /home/askar/.rvm/rubies/ruby-1.9.3-p429 so the load path should be correct.
Your problem is that you used sudo which will do a gem installation for the system ruby. Try again without sudo, just
gem install nokogiri
to install gems for the current rvm ruby.

Related

Install ruby gem in rescue block when 'LoadError' occurs

I am trying to install a ruby gem from a ruby script when it can't load the required gem from local system. Here is my code.
begin
require '<gem name here>'
rescue LoadError
puts `gem install <gem name here>`
require '<gem name here>'
end
The code above installs the gem in the rescue block. But when it requires the gem, it shows this error:
.rvm/rubies/ruby-2.4.2/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb
:55:in require': cannot load such file -- <gem name here> (LoadError)
How can this be solved? I want the gem to be loaded within the rescue block if not already present in the system.
After rigorous searching, I found an answer. If we use Gem.clear_paths after installing the gem, it will now available to the script. Total updated code is :
begin
require '<gem name here>'
rescue LoadError
puts `gem install <gem name here>`
Gem.clear_paths
require '<gem name here>'
end

How to unzip a file with ruby

Okay I've found the following code for unzippping a file with Ruby.
def unzip_file (file, destination)
Zip::ZipFile.open(file_path) { |zip_file|
zip_file.each { |f|
f_path=File.join("destination_path", f.name)
FileUtils.mkdir_p(File.dirname(f_path))
zip_file.extract(f, f_path) unless File.exist?(f_path)
}
}
end
Above this I'm using the following to ensure the needed gems are installed.
begin
require 'rubygems'
rescue LoadError
'gem install rubygems'
end
begin
require 'zip/zip'
rescue LoadError
'gem install rubyzip'
end
So when I call unzip_file I get the following error:
in `unzip_file': uninitialized constant Zip (NameError)
What am I doing wrong?
Thanks!
Beware: the sample script will also unpack symlinks, and will unpack ../../../../etc/passwd without complaining. The rubyzip gem expects you to do your own pathname laundering.
Note that in rubyzip 1.1.4, Zip::Zipfile was renamed to Zip::File.
The problem with installing the gem that way is that you're shelling out to another process with:
`gem install rubyzip`
and after that finishes installing the gem, your current irb session still won't see it. You'd have to reload irb with exec "irb" and then calling require 'zip' again.
Note: those are backticks not single quotes.
Try this:
begin
require 'zip'
rescue LoadError
`gem install rubyzip`
exec "irb"
retry
end
For me require 'zip' works. I have rubyzip-1.1.2
Now you should be able to use Zip
Also, the gem command is rubygems. So you can't install rubygems with itself. It should already be installed, but if not try this: http://rubygems.org/pages/download

Cannot find octokit gem when running ruby, but irb can find it

I ran rvm implode to get a fresh install, then reinstalled RVM and ruby. Then I installed the octokit gem which I want to run.
When I run require 'octokit' in irb everything works, but when I try it from the command line, like such:
ruby file.rb where file.rb is:
require 'octokit.rb'
require 'csv.rb'
CSV.open("node_attributes.csv", "wb") do |csv|
csv << [Octokit.user "dbussink"]
csv << [Octokit.user "sferik"]
end
I get:
1:in require: no such file to load -- /octokit (LoadError)
which ruby yields /usr/bin/ruby, which irb yields /usr/bin/irb, but which octokit yields octokit not found.
Further, rvm list yields:
rvm rubies
=* ruby-2.0.0-p247 [ x86_64 ]
# => - current
# =* - current && default
# * - default
And, gem list octokit yields:
*** LOCAL GEMS ***
octokit (1.25.0, 1.4.0)
How can I make sure that I can find octokit when running ruby from the command line? I've tried changing the path, using an explicit path in the require command, etc., but nothing seems to work!
You need to import require 'octokit' not require 'octokit.rb'. I see why you'd require the latter (name of the project on GitHub).

How do you use ruby gems after they have been installed on OS X

I am able to install the json library using ruby gems. In case it's relevant, I'm using the latest OS X.
usr$ sudo gem install json
Password:
Building native extensions. This could take a while...
Successfully installed json-1.6.5
1 gem installed
Installing ri documentation for json-1.6.5...
Installing RDoc documentation for json-1.6.5...
However, attempting to require 'json' it doesn't work. What am I doing wrong?
touakas-MacBook-Pro:tmp jacob$ ruby x.rb
x.rb:3:in `require': no such file to load -- json (LoadError)
from x.rb:2
The x.rb is as follows:
#!/usr/bin/env ruby
require 'json'
x = { "a"=>"b" }
print x.to_json
You need to load RubyGems first:
require 'rubygems'
require 'json'
Edit:
According to the comment below, you'd better use ruby -rubygems x.rb rather than require rubygems directly (suppose that you're writing a module for resue).
Credit to: #injekt and #Ingenu

ruby: code to install gem if missing

is there some ruby code I can use to install a gem from a local file, if that gem is not installed?
i'm thinking it would look something like:
if !gem_installed("some gem name")
system "gem install -l local_copy.gem"
end
i don't know if anything exists that lets me check for gems like this or not...
Checking availability is covered in this previous StackOverflow Quesiton
begin
gem "somegem"
# with requirements
gem "somegem", ">=2.0"
rescue Gem::LoadError
# not installed
end
or
matches = Gem.source_index.find_name(gem.name, gem.version_requirements)
As for the install, it looks like rails uses the system for gem install also
puts %x(#{cmd})
This is my way of doing this
['json','date','mail'].each { |req|
begin
gem req
rescue Gem::LoadError
puts " -> install gem " + req
Gem.install(req)
gem req
end
require req
}

Resources