require 'rubygems'

24,636

Solution 1

It is often superfluous. It will allow you to require specific versions of particular gems though, with the gem command.

https://guides.rubygems.org/patterns/#requiring-rubygems

Solution 2

require 'rubygems' will adjust the Ruby loadpath allowing you to successfully require the gems you installed through rubygems, without getting a LoadError: no such file to load -- sinatra.

From the rubygems-1.3.6 documentation:

When RubyGems is required, Kernel#require is replaced with our own which is capable of loading gems on demand.

When you call require 'x', this is what happens:

  • If the file can be loaded from the existing Ruby loadpath, it is.

  • Otherwise, installed gems are searched for a file that matches. If it's found in gem 'y', that gem is activated (added to the loadpath).

The normal require functionality of returning false if that file has already been loaded is preserved.

See the documentation for Kernel#require to understand why this is necessary.

Solution 3

As an addition to prior (and correct answers): Ruby 1.9 and newer ship with RubyGems built-in, so there is no real need to require 'rubygems'. Source here

Share:
24,636
ceth
Author by

ceth

I'm a programmer, occasional SRE, Unix automator. I'm currently working primarily in Go, and prior to that my weapon of choice was Python, but I'm also familiar with Java, C#, JavaScript, and bash. I dabble in Clojure and F# but I've never thrown a big problem at it. I run Linux at home and at work but that doesn't mean I'm ignorant of other systems :)

Updated on May 22, 2020

Comments

  • ceth
    ceth almost 4 years

    I have seen many samples of Ruby code with this line (for example, http://www.sinatrarb.com/). What is purpose of this require?

    # require 'rubygems'
    require 'sinatra'
    get '/hi' do
      "Hello world!"
    end
    

    In all cases the code works without this line.

  • Carmine Paolino
    Carmine Paolino about 14 years
    Btw, I don't recommend it. See this post by Ryan Tomayko: gist.github.com/54177
  • Dan Rosenstark
    Dan Rosenstark about 14 years
    @Carmine Paolino, regarding your comment: Sinatra is a gem. What is the best way to make sure that requiring it does not produce an error without using require 'rubygems'?
  • PJP
    PJP about 14 years
    In Ruby 1.9 'require "rubygems"' happens automatically. In previous Rubies rubygems is not automatically required and you will get the error. Your best bet is to automatically type it in unless you are sure you are running always in 1.9+. Ruby won't care if you require it twice in 1.9+ and your code will be happy in 1.8.whatever.
  • Carmine Paolino
    Carmine Paolino about 14 years
    @yar: The post I linked presents 3 solutions.
  • Carmine Paolino
    Carmine Paolino about 14 years
    @Greg: quoting Ryan Tomayko: "The system I use to manage my $LOAD_PATH is not your library/app/tests concern. Whether rubygems is used or not is an environment issue. Your library or app should have no say in the matter. Explicitly requiring rubygems is either not necessary or misguided."
  • Dan Rosenstark
    Dan Rosenstark about 14 years
    @Carmine Paolino, seems like all of the three solutions require rubygems, just indirectly.
  • Carmine Paolino
    Carmine Paolino about 14 years
    @yar: in fact they are solutions to the problem (in your own words) "the best way to make sure that requiring it does not produce an error without using require 'rubygems'". If you don't use rubygems you just use Kernel#require which is quite straightforward...
  • lkahtz
    lkahtz almost 13 years
    RubyGems will select the latest installed version of the gems that follow. If no such software is found, a exception is generated.
  • oreoshake
    oreoshake over 12 years
    I took the -rubygems option, was easy to do for my command line utility: #!/usr/bin/env ruby -rubygems
  • PJP
    PJP over 11 years
    Why is it superfluous? It's the best way to load a needed gem.
  • Jakob Kruse
    Jakob Kruse about 11 years
    Actually, "require 'rubygems'" does not load a needed gem, it only modifies the behaviour of the "require" method.
  • xoryves
    xoryves over 6 years
    You are right, as long as ruby is not build with --disable-rubygems.