Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

require 'rubygems'

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.

like image 259
ceth Avatar asked Apr 26 '10 07:04

ceth


People also ask

What is require RubyGems?

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 .

How do I get RubyGems?

Open up the 'Software Center' app from your launcher and type in `RubyGems` without quotes into the application search box at the top right, and press [enter]. RubyGems then can be installed by just clicking on the button labeled 'Install', thats it.

What is RubyGems Linux?

RubyGems is a system for managing Ruby software libraries. Ruby code packaged in this manner is called a gem. When you find Ruby software you want to use in a project, gems offer a means of downloading, installing and managing the software. History.


1 Answers

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.

like image 124
Carmine Paolino Avatar answered Sep 21 '22 20:09

Carmine Paolino