ruby install yob pdf reader - ruby

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'

Related

HTTParty won't load in Ruby file

I am trying to run a webscraper and when I execute the file I get the following error
/home/luis/.rbenv/versions/2.4.1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- httparty (LoadError)
from /home/luis/.rbenv/versions/2.4.1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from luismejia71lbs.rb:1:in `<main>'
I have HTTParty installed and even ran sudo gem install httparty a few times just in case. Not sure why it won't run and I can't find anything concrete. I recently installed rbenv in the last week, not sure if that is related, but since I was able to run httparty in the past.
Here is my file
webscraper.rb
require 'httparty'
require 'nokogiri'
require 'pry'
require 'json'
require 'csv'
craigslist = HTTParty.get('https://miami.craigslist.org/search/mdc/apa?min_bedrooms=3&max_bedrooms=3&min_bathrooms=2&max_bathrooms=2&availabilityMode=0')
Pry.start.(binding)
The error means rbenv can't find the httparty gem. Since rbenv installs gems under ~/.rbenv, you shouldn't need the sudo command to install httparty. Using sudo will almost certainly install in the wrong place. Just do:
$ gem install httparty

How to write a batch script to install ruby gem

I'm writing a batch script as a setup for a ruby program I'm writing. It needs to be able to
a. Make sure Ruby is installed on the user's computer (and if not point them to the ruby download page)
b. make sure the ruby "yaml" gem is installed, which is a prerequisite for it. I've tried
gem install yaml
in the batch script to no avail. How can I write a batch script that will do these two things?
As far as I know, there is no yaml gem. Although it must be required in code that uses it, it is distributed as part of a Ruby installation. Try this:
ruby -ryaml -e"puts 'YAML found'"
It should work; and if you change the -r token to some nonexistent gem, you'll see an exception raised.
Instead of writing your own script, you could use bundler and create a gemfile. This way people can install all the gems on any operating system.
Example:
require 'rubygems'
require 'bundler/setup'
require 'nokogiri'
require 'rest-client'
#require all your gems like normal
def parse(site)
Nokogiri::HTML(RestClient.get(site))
end
And for the gem file:
source: "https://rubygems.org"
gem 'nokogiri', '~> 1.6.7.2' #<= you can specify which version
gem 'rest-client' #<= you don't have to specify a version though
After you've got everything set up, cd to the directory that has the gemfile and run bundle install this will install all the gems

Ruby: require downloaded gems

I have finished writing a ruby script which I would like to share with others, but I am having trouble getting the user to install the necessary gems. I have tried 2 approaches, and a fix for either of them would be greatly appreciated! I require the following gems:
require 'rubygems'
require 'highline/import'
require 'mechanize'
I have tried the following:
1) Generate a stand-alone app with Platypus. I created the Gemfile:
source "https://rubygems.org"
gem "highline", "~> 1.6.20"
gem "mechanize", "~> 2.7.3"
and bundle installed it and included require 'bundler/setup'. I uploaded Gemfile.lock and the ruby script but I receive this error when I run it:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in require': cannot load such file -- highline/import (LoadError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:inrequire'
from /Users/jonathanli/Documents/CS_projects/iSites/Isites.app/Contents/Resources/script:5:in `'
2) I downloaded the gem files and placed them directly in my host folder. I am unsure of how to modify my require statements, but it is not working as it stands right now.
Thanks in advance everyone! Hope this was enough detail.

Writing to an XML file in JRuby using Builder

I'm trying to write to an XML file using Builder but cannot seem to find Builder when I start up my program. I installed it using gem install builder and then used the following in my file, but it does not start up:
require 'rubygems'
require_gem 'builder'
def product_xml
builder = Builder::XmlMarkup.new("", 2)
puts builder.person {
name("Selene")
id("1")
x("2");
y("3");
}
end
The error that I am getting is:
LoadError: no such file to load -- rubygems
require at org/jruby/RubyKernel.java:1038
(root) at C:\Users\Ron\Dropbox\...\server\.\data\plugins\cmd-dev\dev.r
b:2
To install gems inside the JRuby enviroment you must use jruby -S gem, not just gem (if you are not using RVM).
So you should install Builder with jruby -S gem install builder. Once you have installed it, you should just use require 'builder', not require_gem 'builder'.

require nokogiri: Load Error

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.

Resources