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

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

Related

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

No need to first require 'rubygems'?

I am currently reading through this guide: http://guides.rubygems.org/what-is-a-gem/
Under the third section ("Requiring code"), it says that if I run require 'rake', it should fail, because "because RubyGems isn’t loaded yet.". However, when I run require 'rake', (without running require 'rubygems', it works! Is the guide incorrect or am I using a wrong version of Ruby or has something changed?
You are probably running Ruby 1.9.
Per The RubyGems User Guide:
The default Ruby 1.9 package now
includes RubyGems by default on most
platforms (presently Debian based
systems split this out into a separate
package). This means that on Ruby 1.9
and above, you will not need to
require 'rubygems' in order to load
gem libraries.
If you have RUBYOPT=rubygems set in your environment when you launch Ruby, then Ruby Gems will automatically be required. Or if you launch ruby with the argument -rubygems, which is equivalent. Or if you're using Ruby 1.9 or later, Rubygems should automatically be required.
One (or more) of these things is probably true on your system.

Ruby: How to include/install .bundle?

I'm new to Ruby. I have a .bundle file. I put it in the source folder and did
require('my.bundle')
But when I call the methods in the bundle, the definition is not found. Do I have to install them or include them in some other way to access them?
I am on Ruby version 1.8.7 (latest version on Mac).
I highly recommend using RVM to manage your Ruby installation, including your gems, so if you don't already have that, get it and follow the instructions for installing it. Make sure you do the part about modifying your bash startup script or you'll see weird behavior, like the wrong Ruby being called. Also, use the steps in "RVM and RubyGems" to install your gems or you can run into weird behavior with gems being installed under the wrong or an unexpected Ruby.
Second, use the gem command to install gems:
gem install gem_to_install
replacing "gem_to_install" with the name of the gem you want, and it will be installed into the appropriate gem folder for your Ruby.
If you are on Ruby 1.92, and trying to require a gem to use as a module in your code, use:
require 'gemname'
if it is installed via the gem command. And, if it is a module you wrote or have in your program's directory or below it, use:
require_relative 'path/to/gem/gemname'
If you are on a Ruby < 1.9 you'll also need to add require 'rubygems' above your other require lines, then use require './path/to/gem/gemname'.
Thanks, but my .bundle is not in gems. How do I install/require a .bundle file I already have?
If you wrote it look into rubygems/gemcutter or bundler for info on bundling and managing gems.
You can install a gem without using the app by going into the directory containing the gem and running setup.rb. See http://i.loveruby.net/en/projects/setup/doc/usage.html for a decent writeup or the official docs at: http://docs.rubygems.org/read/chapter/3

Using installed gems in Ruby with 'require'

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.

I have a gem installed but require 'gemname' does not work. Why?

The question I'm really asking is why require does not take the name of the gem. Also, In the case that it doesn't, what's the easiest way to find the secret incantation to require the damn thing!?
As an example if I have memcache-client installed then I have to require it using
require 'rubygems'
require 'memcache'
My system also doesn't seem to know about RubyGems' existence - unless I tell it to. The 'require' command gets overwritten by RubyGems so it can load gems, but unless you have RubyGems already required it has no idea how to do that. So if you're writing your own, you can do:
require 'rubygems'
require 'gem-name-here'
If you're running someone else's code, you can do it on the command line with:
ruby -r rubygems script.rb
Also, there's an environment variable Ruby uses to determine what it should load up on startup:
export RUBYOPT=rubygems
(from http://www.rubygems.org/read/chapter/3. The environment variable thing was pointed out to me by Orion Edwards)
(If "require 'rubygems' doesn't work for you, however, this advice is of limited help :)
There is no standard for what the file you need to include is. However there are some commonly followed conventions that you can can follow try and make use of:
Often the file is called the same
name as the gem. So require mygem
will work.
Often the file is
the only .rb file in the lib
subdirectory of the gem, So if you
can get the name of the gem (maybe
you are itterating through
vendor/gems in a pre 2.1 rails
project), then you can inspect
#{gemname}/lib for .rb files, and
if there is only one, its a pretty
good bet that is the one to require
If all of that works, then all you can do is look into the gem's directory (which you can find by running gem environment | grep INSTALLATION | awk '{print $4}' and looking in the lib directory, You will probably need to read the files and hope there is a comment explaining what to do
The require has to map to a file in ruby's path. You can find out where gems are installed by running 'gem environment' (look for INSTALLATION DIRECTORY):
kburton#hypothesisf:~$ gem environment
RubyGems Environment:
- RUBYGEMS VERSION: 1.2.0
- RUBY VERSION: 1.8.7 (2008-08-08 patchlevel 71) [i686-linux]
- INSTALLATION DIRECTORY: /usr/local/ruby/lib/ruby/gems/1.8
- RUBY EXECUTABLE: /usr/local/ruby/bin/ruby
- EXECUTABLE DIRECTORY: /usr/local/ruby/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86-linux
- GEM PATHS:
- /usr/local/ruby/lib/ruby/gems/1.8
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :benchmark => false
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- http://gems.rubyforge.org/
kburton#editconf:~$
You can then look for the particular .rb file you're attempting to require. Additionally, you can print the contents of $: from irb to see the list of paths that ruby will search for modules:
kburton#hypothesis:~$ irb
irb(main):001:0> $:
=> ["/usr/local/ruby/lib/ruby/site_ruby/1.8", "/usr/local/ruby/lib/ruby/site_ruby/1.8/i686-linux", "/usr/local/ruby/lib/ruby/site_ruby", "/usr/local/ruby/lib/ruby/vendor_ruby/1.8", "/usr/local/ruby/lib/ruby/vendor_ruby/1.8/i686-linux", "/usr/local/ruby/lib/ruby/vendor_ruby", "/usr/local/ruby/lib/ruby/1.8", "/usr/local/ruby/lib/ruby/1.8/i686-linux", "."]
irb(main):002:0>
Also rails people should remember to restart the rails server after installing a gem
You need to include "rubygems" only if you installed the gem using gem . Otherwise , the secret incantation would be to fire up irb and try different combinations . Also , you can pass the -I option to the ruby interpreter so that you include the instalation directory of the gem , in the LOAD_PATH .
Note that $LOAD_PATH is an array , which means you can add directories to it from within your script.
The question I'm really asking is why require does not take the name of the gem.
Installing a gem gets the files onto your system. It doesn't make any claims as to what those files will be called.
As laurie points out there are several conventions for how they are named, but there's nothing to enforce that, and many gem authors unfortunately don't stick to them.
Also, In the case that it doesn't, what's the easiest way to find the secret incantation to require the damn thing!?
Read the docs for your gem?
I find googling for rdoc gemname will usually find the official rdocs for your gem, which usually show you how to use it.
Memcache is perhaps not the best example, as they assume you'll be using it from rails, and the 'require' will have already been done for you, but most other ones I've seen have examples which show the correct 'require' incantations
I had this problem because I use rvm and was trying to use the wrong version of ruby. The gem in question needed 1.9.2 and I had set 2.0.0 as my default! Maybe a dumb error but one that someone else arriving on this page will probably have made.
An issue I just ran into was that the actual built gem was not including all the files that it should have.
The issue with files was that there was a syntax mistake in the in the gemspec, but no errors were thrown during the build.
Just adding this here in case anybody else runs into the same issue.
It could also be the gem name mismatch:
e.g.
dummy-spi-0.1.1/lib/spi.rb should be named dummy-spi-0.1.1/lib/dummy-spi.rb
then you can
require 'dummy-spi'
I too had this problem since installing OS X Lion, and found that even if I ran the following code I would still get the warning message.
require 'rubygems'
require 'nokogiri'
I tried loads of solutions posted here and on the web, but in the end my work around solution was to simply follow the instructions at http://martinisoftware.com/2009/07/31/nokogiri-on-leopard.html to reinstall LibXML & LibXSLT from source, but ensuring the version of LibXML I installed matched the one that was expected by Nokogiri.
Once I had done that, the warnings went away.
Watch source of gem and check lib directory. If there is no rb file then you must point to gem main rb file in subdirectory:
require 'dir/subdir/file'
for /lib/dir/subdir/file.rb.

Resources