Using installed gems in Ruby with 'require' - ruby

I do something like sudo gem install json. Then I do irb. Then I do require 'json'. Then it says no such file to load -- json

You need to make sure that RubyGems itself is loaded, for requiring gems to work.
There are several ways to do this. You could require 'rubygems' explicitly in each file you want to use the gems in, but that might get to be a pain. Or you could pass -rubygems into ruby when you execute it, but again, you'd need to remember to do that each time.
The best way would probably be to set the RUBYOPT environment variable to rubygems. For instance, you may add the following line to your .profile:
export RUBYOPT=rubygems

Try adding this first:
require 'rubygems'
RubyGems is a dependency manager as well as an installer.

Related

When is the 'require' necessary when using a ruby gem?

I noticed for some gems you must include it in the file where you want to use it like this require 'a_gem', but this is not always the case.
I am going to compose a gem by myself. What should I do if I do not want to add the require 'my_gem' to the .rb file when using it?
Usually, an application that is using a gem needs to require the gem:
require "my_awesome_gem"
MyAwesomeGem.do_something_great
However, if an application is using bundler, which defines the application's gem in a file called "Gemfile":
source 'http://rubygems.org'
gem 'my_awesome_gem'
then the application may invoke bundler in a way that automatically requires all of the gems specified in the Gemfile:
require "bundler"
Bundler.require
MyAwesomeGem.do_something_great
Rails projects use Bundler.require, so a Rails application will not need to explicitly require a gem in order to use it: Just add the gem to the Gemfile and go.
For more about Bundler.require, see the bundler documentation
For more about how Rails uses Bundler, see How Does Rails Handle Gems? by Justin Weiss.
This doesn't make sense. If you want to write a Gem and use it, it needs to be required somewhere.
This "somewhere" could be explicit in one of your scripts, it could be written in a Gemfile or it could be required by another script/gem that is required by your script.
If you write a gem, Ruby will not include it automatically in all your scripts.
Finally, if you publish it, should every single Ruby script on earth magically require it and execute your code?
I suppose that the project you have seen which didn't use require 'a_gem' was using a Gemfile.

Nokogiri Ruby 'require' Issues

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

Require a gem library

I'm kinda new to ruby and I'd like to use a gem charting library, but for some reason when I require it in the ruby script on my desktop it doesn't work. However when I require in my irb it does work. Is there a way to fix this?
Try using bundler if you can and declare the requirements in a Gemfile. This will make your environment much more consistent between different computers and will provide a reference of the dependencies you have.
The Bundler setup procedure is pretty simple and well documented. It will load in all the gems and any of their dependencies automatically.
Generally the problem with the require statement failing is the library is not in your $LOAD_PATH, and that's usually because you haven't loaded rubygems:
require 'rubygems'
gem 'somegem'
require 'somegem'

When do we need to "require 'rubygems'"?

This is related to this question:
https://stackoverflow.com/questions/3179797/how-to-use-rubytorrent-or-other-gems
I thought RubyGems is a package manager, similar to apt-get on Ubuntu...
So when do we need to require 'rubygems' in our code?
Use require 'rubygems' when you are using a gem that you installed with Rubygems. The reason is that Ruby uses Rubygems to determine the path of the gem that Rubygems installed. (is unable to locate the gem you want to use)
Alternatively, you can pass the -rubygems flag when you invoke your script, or set export RUBYOPT=rubygems in your profile (~/.bashrc or ~/.bash_profile or ~/.profile) which is basically the same as the flag, except it is implicit.
On 1.9, rubygems is required implicilty, and you shouldn't have to do that.
Here are some docs about it http://docs.rubygems.org/read/chapter/3
Note: Some have built solutions (zozo and faster_rubygems) to avoid Rubygems overhead http://www.ruby-forum.com/topic/212463

Ruby Package Include Problems

I'm trying to use the Optiflag package in my Ruby code and whenever I try to do the necessary require optiflag.rb, my program fails with the standard no such file to load -- optiflag message. I added the directory with that library to my $PATH variable, but it's still not working. Any ideas?
is it a gem? Are you doing
require 'rubygems'
require 'optiflag'
or equivalent?
It looks like it's a gem, so you need to enable ruby gems before requiring it.
This site explains many ways of how to do it. But to have the cheat sheet here these are:
1) Require the rubygems package before using a gem.
require "rubygems"
require "optiflag" # etc
2) Add the -rubygems flag to wherever you execute ruby. I.e:
ruby -rubygems Something.rb
3) Add an environment variable called RUBYOPT, giving it an option of rubygems. I.e:
RUBYOPT=rubygems
I also keep having this problem with RubyXL, tried to use single and double quotes. Is there something else that needs to be done? Maybe putting a file somewhere? I already succesfully installed the gem with sudo gem install rubyXL (RubyXL actually din't work).

Resources