I'm new to Ruby and I was looking around for libraries to parse html and found Nokogiri. The problem I'm having is if I fire up IRB and type require 'nokogiri', it works fine and prints out true. But if I include that as a part of my .rb file and execute it with ruby <scriptname>, it gives me a LoadError: no such file to load message.
My Ruby version is 1.8.7 if I type ruby --version in the terminal.
Try adding
require 'rubygems'
to the .rb file.
In Ruby 1.9+ rubygem is loaded automatically and not needed. Although there is debate whether that line is required pre 1.9.
Nevertheless, give it a shot.
Related
I am trying to create gem using bundler. This gem requires pp gem to make 'pretty print'. I have require 'pp' at the top of the source and after that I use pp where needed. However, a runtime error occurs.
D:/PRJ/git/smde/vendor/bundle/ruby/2.5.0/gems/pp-0.1.1/lib/pp.rb:1:in `require': cannot load such file -- pp/room (LoadError)
There is no room file in lib/pp directory in pp gem. Why?
What is more interestingly, pp gem works well when I start my gem scripts directly, i.e. "ruby myscript.rb". The lack of pp/lib/room is not essential.
The "pp" gem is not required for using pretty print. That gem is related to Campfire, which does have the concept of a room. See https://www.rubydoc.info/gems/pp/0.1.1/Pp
Pretty print is available to you without requiring anything: notice if you run irb, you can immediately type
pp "something"
And it will print as you want.
I'm new to Ruby and I'm having a lot of trouble trying to use Nokogiri. I've been trying to find a resolution for hours now, so any help is appreciated. I tried searching for and using solutions from other related SO posts before caving and posting my own. When I run ruby -v I get: ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]
(Edit: I have updated ruby with updates-alternatives --config ruby and selected /usr/bin/ruby1.9.1 but when I do ruby -v it is now showing version 1.9.3 WTF am I doing wrong here?)
I have a new project directory at ~/workspace/ruby/rubycrawler/ and I used Bundler to install nokogiri, which installed correctly:
Using mini_portile (0.5.2)
Using nokogiri (1.6.1)
Using bundler (1.5.1)
Your bundle is complete!
Running bundle show nokogiri returns /var/lib/gems/1.9.1/gems/nokogiri-1.6.1.
In the directory I'm running the script from I have a simple html file named "index.html". The script I'm trying to run is even simpler (or so I thought):
require 'nokogiri'
page = Nokogiri::HTML(open("index.html"))
puts page.class # Nokogiri::HTML::Document
The error is rubycrawler.rb:1:in 'require': no such file to load -- nokogiri (LoadError).
I also added require 'rubygems' even though I read it isn't needed for 1.9+ and still no luck.
A lot of searching shows "Did you put this gem in your Gemfile?". So I generate a Gemfile and add gem 'nokogiri'. I try running the small script again and get the same error. I read "Try deleting Gemfile.lock." so I did but still couldn't get it to work. I then read to try testing it out in irb so I tested "open-uri" and "nokogiri" and here's what I got:
irb(main):001:0> require 'open-uri'
=> true
irb(main):003:0> require 'nokogiri'
LoadError: no such file to load -- nokogiri
I'm really having a lot of trouble figuring this out, so really any help at all is really appreciated.
Ruby tools like RVM, Bundler, etc., to the novice, appear to do a lot of magic, but really, there is no magic to them. The key here lies in what Bundler actually does for you. It manages a manifest of dependencies, BUT at runtime, those dependencies STILL have to get loaded somehow, and my gut feeling is that is what is not happening here.
Regardless of what version of Ruby you are using, if you are using Bundler, there's an easy way to do this. Precede the command that starts your program with "bundle exec" and that will make Bundler edit Ruby's load path so that it includes all the things in the manifest (Gemfile.lock).
For example:
$ bundle exec ruby foo.rb
A additional note for anyone using RVM: RVM generally will modify the shebangs in the scripts that launch programs like "ruby" or "rake" so that they use the "ruby_no_exec" shell (or similar) instead of the plain old "ruby" shell. That alternate shell is Bundler-aware and makes it generally unnecessary to type "bundle exec," but since the OP is using system Ruby, that's not applicable and commands should be manually prefixed with "bundle exec".
Hope this helps!
In addition to Kent's answer, I would recommend switching to RVM instead of using the system installed ruby. System rubies tend to be horribly out of date, especially when it comes to important things like features and security updates. It might not help you in your current situation, but it would be well worth the time. If you are unfamiliar: http://rvm.io
I'm trying to install the yob pdf reader: https://github.com/yob/pdf-reader#readme
I'm following the directions there and do:
gem install pdf-reader
and that reports success.
I place a pdf in my directory, name it 1.pdf.
Then I create a script that has the following code:
reader = PDF::Reader.new("1.pdf")
when I run that script I get the following error:
uninitialized constant PDF (NameError)
Googling it hasn't been very successful so far.
How do I solve this?
If it's a pure ruby script, you may be missing the following at the beginning:
require 'rubygems'
require 'pdf-reader'
You can also specify the version of the gem that you want to require, in case you have multiple versions installed:
require 'rubygems'
gem 'pdf-reader', "~> 0.10.0"
require 'pdf-reader'
I have seen many samples of Ruby code with this line (for example, http://www.sinatrarb.com/). What is purpose of this require?
# require 'rubygems'
require 'sinatra'
get '/hi' do
"Hello world!"
end
In all cases the code works without this line.
require 'rubygems' will adjust the Ruby loadpath allowing you to successfully require the gems you installed through rubygems, without getting a LoadError: no such file to load -- sinatra.
From the rubygems-1.3.6 documentation:
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.
See the documentation for Kernel#require to understand why this is necessary.
It is often superfluous. It will allow you to require specific versions of particular gems though, with the gem command.
https://guides.rubygems.org/patterns/#requiring-rubygems
As an addition to prior (and correct answers): Ruby 1.9 and newer ship with RubyGems built-in, so there is no real need to require 'rubygems'. Source here
Has anyone used Watir with IronRuby successfully? I am getting an error that the required file 'Watir' was not found. What path do I need to set to get this file to work in IronRuby?
For some reason my igem command is not working:
C:\DevTools\IronRuby\ironruby\Merlin\Main\Languages\Ruby\Scripts\bin>igem instal
l watir
'"C:\DevTools\IronRuby\ironruby\Merlin\Main\Languages\Ruby\Scripts\bin\ir.exe"'
is not recognized as an internal or external command,
operable program or batch file.
I am using 0.9 version of Ironruby.
I remember that in 0.9 you have to indicate the ir tool: I used the following and got the error again!
C:\DevTools\IronRuby\ironruby\Merlin\Main\Languages\Ruby\Scripts\bin>ir igem ins
tall watir
ERROR: While executing gem ... (RangeError)
bignum too big to convert into Fixnum
The current version of RubyGems is 1.3.5:
C:\DevTools\IronRuby\ironruby\Merlin\Main\Languages\Ruby\Scripts\bin>ir igem -v
1.3.5
I even tried using the full path:
require File.dirname(__FILE__) + "C:/ruby/lib/ruby/gems/1.8/gems/commonwatir-1.6.2/lib/watir.rb"
Did you use gem install watir or igem install watir? If you are installing gems for IronRuby you have to use igem. Otherwise, it ends up being put inside the gems directory where your Ruby installation resides. IronRuby will not see that gems directory by default and you would have to use the full path to get to it. When using igem it puts the gem in the correct directory for use with IronRuby.
Watir uses MRI Ruby's WIN32OLE library. Is this library supported with Iron Ruby?
I found that when you get the required watir not found message in regular ruby you need to put before require 'watir' the text require 'rubygems'