On Ruby: uninitialized constant Nokogiri::ClassResolver (NameError) - ruby

I've this issue currently when launching my .rb, that says 1: from /var/lib/gems/2.7.0/gems/nokogiri-1.13.1-x86_64-linux/lib/nokogiri/xml/node.rb:56:in `<module:XML>' /var/lib/gems/2.7.0/gems/nokogiri-1.13.1-x86_64-linux/lib/nokogiri/xml/node.rb:59:in `<class:Node>': uninitialized constant Nokogiri::ClassResolver (NameError)
I've put theses lines in my .rb :
require 'rubygems'
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open('http://www.google.com/search?q=doughnuts'))
puts doc
and my Gemfile contains this :
source 'https://rubygems.org'
ruby '2.7.4'
gem 'rspec'
gem 'nokogiri'
gem 'open-uri'
Does anyone have any idea about the problem or oversight that is causing this error?
Thanks!

The "nokogiri" package updates version might have bugs so try to install lower version of nokogiri package after removes the new version....Try this,
I am not sure about it, I have read it in one senior developer post.

The following fixed it for me:
sudo apt-get remove ruby-nokogiri

This solves the problem for me:
require 'nokogiri/class_resolver'
require 'nokogiri'
Requiring the file that defines the constant (class_resolver) before the file that uses it makes it available, and therefore no error.

Related

Bundler issue when running a ruby script

I am trying to run some older ruby script (with old ruby version) from within a ruby script. Here is a program:
old_ruby187.rb
#!/usr/ruby/1.8.7/bin/ruby
puts "Hello"
new_ruby230.rb
#!/usr/ruby/2.3.0/bin/ruby
require "rubygems"
require "bundler"
Bundler.setup # Code works if I comment this line
puts `old_ruby187.rb`
I get bundler load error. If I execute ./new_ruby230.rb, it gives an error for the last puts command line.:
'require': no such file to load -- rubygems (LoadError)
If I comment just Bundler.setup and run it, it works fine. Not sure if Bundler.setup tries to load something for system call. I need bundler for other gems used in new_ruby230.rb script.
Any help is appreciated.
Update (02/22/2018):
I ended up using ssh when calling old ruby script. Something like:
new_ruby230.rb
#!/usr/ruby/2.3.0/bin/ruby
require "rubygems"
require "bundler"
require "socket"
Bundler.setup
puts `ssh #{Socket.gethostname} old_ruby187.rb` # this worked!
Pretty sure you're not supposed to require rubygems like that. Bundler is there to manage your gems. Remove that require statement (require "rubygems") and you should be good to go.

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!

Why isn't 'bundler/setup' including any of the gems in my Gemfile?

I'd like to use bundler/setup to include all of my listed gems but I'm not
succeeding. In go.rb I have
require 'rubygems'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
which fails to require httparty as I thought it would:
$ bundle exec ruby go.rb
go.rb:5:in `<main>': uninitialized constant HTTParty (NameError)
What am I doing incorrectly?
I've created a small project for this question, here.
As far as I understand 'bundler/setup' it only manages the require path (removes the default contents and adds paths for gems that are defined in Gemfile.lock). If you don't require the libraries in question, their contents won't be available.
I believe the problem in your particular case is the following snippet:
File.expand_path('Gemfile', __FILE__)
If I run this from a file called /foo/bin/somescript, what the above code expands to is /foo/bin/somescript/Gemfile. Presumably what you actually want is /foo/bin/Gemfile, which you can get with:
File.expand_path('../Gemfile', __FILE__)
So, repeating your original code with the correction:
require 'rubygems'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
This seems to work for me, to have files in my ~/bin directory have access to libraries I've installed by running bundle install within that directory, as specified by the Gemfile therein.

ruby install yob pdf reader

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'

How to set specified gem version for Ruby app?

I`ve encountered the problem after updating some gems, so basically all older gems are still available but i cant force application use them.
Lets say, i need something like that:
require 'rubygems'
require 'mygem', '0.1.2'
Is there a way to do it?
Well, always happens to me. Found an answer.
require 'rubygems'
gem 'mygem', '=0.1.2'
require 'rubygems'
require 'activerecord', '=1.15.3'
This worked for me.
Check this out. Seemed helpful
Link

Resources