How can I use Nokogiri in irb? - ruby

I am trying to:
require 'nokogiri'
in irb, without success. The Nokogiri gem is installed. From:
gem list --local
I get:
nokogiri (1.4.4, 1.4.3.1)
but when I try to "require" it in irb, I get:
LoadError: no such file to load -- nokogiri
from (irb):8:in `require'
from (irb):8
from :0
Nokogiri 'lives' in:
/opt/ruby-enterprise-1.8.7-2010.02/lib/ruby/gems/1.8/gems/nokogiri-1.4.4/lib
on my system. Also, my GEM PATH (from gem env) is:
/opt/ruby-enterprise-1.8.7-2010.02/lib/ruby/gems/1.8
If I go all the way into the nokogiri gem directory, I can successfully "require" it. But why can't I require it from anywhere else? I am misunderstanding something about the gem path.

Try to require 'rubygems' before requiring nokogiri. If there are no witches on your machine this could help.

Ruby prior to 1.9 didn't automatically do a require 'rubygems' for you. Post 1.9 it does. I always forget when I jump back to 1.8.7 to test something, especially in irb.
You can add require 'rubygems' to your ~/.irbrc file if you want. It won't hurt anything having it there.

Related

Require order in a ruby script

I have a ruby script that requires 3 gems to work. My script starts as follows:
#!/usr/bin/env ruby
require 'httparty'
require 'imgkit'
require 'twitter'
Now, interestingly, the above code works, but only if I require httparty first or second. If I require it as the third dependency, I get the following:
'require': cannot load such file -- httparty (LoadError)
I'd love to learn why this is happening, so I can better understand how ruby handles gem dependencies. Many thanks!
Edit: I am using bundler. This is my Gemfile:
source 'https://rubygems.org'
ruby '2.2.2'
gem 'wkhtmltoimage-binary'
gem 'imgkit'
gem 'twitter'
gem 'rspec'
gem 'httparty'
Following tadman's advice, adding require 'bundler/setup' solved the problem. More in the docs. Thanks!

Problems with ruby 'unroller' gem

I'm having a problem using unroller. I have installed the gem and wrote this simple program to help focus on the problem i'm having:
#!/usr/bin/ruby
require 'rubygems'
require 'unroller'
Unroller::trace
def foo(p1, p2)
puts p1
puts p2
end
foo("param1", "param2")
Running the program yields:
/Library/Ruby/Gems/1.8/gems/facets-2.9.3/lib/core/facets/filetest/separator_pattern.rb:5: warning: already initialized constant SEPARATOR_PATTERN
/Library/Ruby/Gems/1.8/gems/facets-2.9.3/lib/core/facets/string/bracket.rb:3: warning: already initialized constant BRA2KET
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `gem_original_require': no such file to load -- facets/methodspace (LoadError)
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `require'
from /Library/Ruby/Gems/1.8/gems/unroller-1.0.0/lib/unroller.rb:4
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:60:in `gem_original_require'
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:60:in `require'
from ./ut:4
My ruby version is ruby 1.8.7 (2011-12-28 patchlevel 357). I also installed ruby on my Windows development box and get the same error and that ruby version is 1.9.3 so it does not appear to be related to the version of Ruby I'm on.
Anyone have any ideas?
Thanks very much in advance!
jon
This is a bug of unroller gem, described here: https://github.com/TylerRick/unroller/issues/1. unroller automatically requires the latest version of facets gem and the version 2.9 breaks it. (BTW gems should never use '>=' when loading dependencies, that's why '~>' is for.)
It's not that difficult to hotfix locally by using bundler and hardcoding facets gem to specific version before requiring unroller (so the specific facets version gets loaded instead of latest 2.9).
create Gemfile:
source 'http://rubygems.org'
gem 'facets', '2.8.4'
gem 'termios' # you're gonna need this gem too, for some reason
gem 'unroller'
run bundle install and then either run the script by bundle exec ruby test.rb or require bundler/setup in it:
require 'rubygems'
require 'bundler/setup'
require 'unroller'
...
UPDATE: or if you don't wanna deal with bundler, try this first, it could work too:
require 'rubygems'
gem 'facets', '2.8.4'
require 'unroller'
...

Missing gem watir?

ahmet#Ubuntu:~/test$ ruby hello.rb
/home/ahmet/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': no such file to load -- watir (LoadError)
from /home/ahmet/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from hello.rb:2:in `<main>'
I have required rubygems in my .rb but still get this error
require 'rubygems'
require 'watir'
According to Watir Readme you have to install and require firewatir. Also you may want to install watir-webdriver instead.
I encountered a similar problem with a different gem a while back on Ubuntu using RVM. I know the gem was installed, because I had just installed it. I did the following:
rvm get latest
rvm reload
gem update --system
Then for good measure I reinstalled the gem, and everything worked. Hope this helps you as well.
Read this: https://github.com/zeljkofilipin/watirbook/blob/master/installation/ubuntu.md
The require should be either 'firewatir' or 'watir-webdriver'
require 'firewatir'
or
require 'watir-webdriver'

require 'nokogiri' issue with Ruby 1.9.2

I have made a Ruby project (not rails project) in Netbeans, and in main.rb file i have been requiring nokogiri but i am getting the following error.
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- nokogiri (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from /home/nadeem/rails_project/RubyApplication1/lib/main.rb:2:in `<main>'
I have installed nokogiri 1.5.0 and using ruby 1.9.2
Any idea?
Using bundler to manage your gem dependencies is probably a good idea because you can check if things are set up properly using bundle check if you've declared and successfully installed them.
The bundler setup routine can import many gems in the proper order, accounting for dependencies. Otherwise you will need to have at least this:
require 'rubygems'
gem 'nokogiri'
require 'nokogiri'
Make sure that the gems are installed with the same version of Ruby you're trying to run. It's easy to get mixed up when you're using rvm and have inadvertently added the gems to a different version.
Refer to this solution hope this will be helpful, Check your net bean's project properties and try to update path from there.
Require Nokogiri? No such file to load

How to install Ruby gem automatically, if required?

Given a Ruby program that uses a particular gem (e.g. term-ansicolor), how could I install the gem automatically, if required?
In other words, the program does:
require 'term/ansicolor'
and, in case the gem wasn't install previously, I would like to install it and continue the program rather than getting an error:
LoadError: no such file to load -- term/ansicolor
from (irb):1:in `require'
from (irb):1
from :0
What would be the most appropriate method to achieve that?
I've been using this pattern:
require 'rubygems'
begin
gem 'minitest'
rescue Gem::LoadError
Gem.install('minitest')
gem 'minitest'
end
require 'minitest'
Such a tool is not available. Read a detailed discussion about it here http://www.ruby-forum.com/topic/2728297
You should consider to use bundler. It's the de-facto standard way to manage dependencies in Ruby software.
Alternatively, you could build your package into a gem and put the required gem as a dependency in the gemspec. It would then get installed automatically when you install the gem.

Resources