require 'rubygems'
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
When do we need to require 'rubygems'?
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
When is the 'require' necessary when using a ruby gem?
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.
Is `require rubygems` returning `false` ok?
It should be fine. Requiring a file the second time results in a false response. With load
, it's a different thing, which would load
(require
) the file each time it is requested.
So it just means that irb is starting with rubygems
already required, which is not a surprise.
For example, load
is heavily used in Rails' development mode, so your changes can immediately be shown.
Since Ruby 1.9 rubygems are automatically required.
Require a specific version of RubyGems
I don't believe it's possible to select from among multiple rubygems versions in a single Ruby installation. But you can ensure that the required version is being used. Put the following somewhere where it will be run early in your application's startup, such as the top of your Gemfile (which is just a Ruby file), or at the beginning of a script which doesn't use bundler:
required_rubygems_version = '2.6.0'
if Gem.rubygems_version < Gem::Version.create(required_rubygems_version)
raise "Please upgrade to rubygems #{required_rubygems_version}"
end
Is this the right way to require ruby gems?
Get rid of these lines from your config.ru file:
require 'rubygems'
require 'bundler'
Bundler.require
Just make sure you run
bundle install
from the terminal to install your gems before starting the application.
How does require rubygems help find rubygem files?
Here's the current version of the relevant source: https://github.com/rubygems/rubygems/blob/02ead548e38ff90923444fa7c0ff9f6a5dbd87b0/lib/rubygems/custom_require.rb. (Edit: here's an earlier version (1.5.2) that more clearly expresses what happens.)
The docs say:
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.
It does this by opening up module Kernel
and aliasing the original require with alias gem_original_require require
, then redefining require
to first call the original version, and look at the gems if that doesn't work.
So the load path is only changed when you require a gem:
ruby-1.8.7-p330 :002 > $:.length
=> 9
ruby-1.8.7-p330 :003 > require 'rubygems'
=> true
ruby-1.8.7-p330 :004 > $:.length
=> 9
ruby-1.8.7-p330 :005 > require 'haml'
=> true
ruby-1.8.7-p330 :006 > $:.length
=> 10
Related Topics
How to Negate a Scope in Rails
Rails 4 Update Nested Attributes
How to Make a Check Box Default to Being "Checked" in Rails 1.2.3
Comma Separated Array with a Text_Field in Rails
Is There a Ruby One-Line "Return If X"
How to Run a Simple Ruby Script in Any Web Server (Apache or Mongrel or Any Thing Else)
Programmatically Getting Full Ruby Version
Dynamically Defined Setter Methods Using Define_Method
Describe VS Context in Rspec. Differences
Convert HTML to Plain Text (With Inclusion of <Br>S)
Change HTML Form Id Generated by Form_For Rails 3.1
Rails Redirect_To :Back Not Working
Rails 3: User Created Custom Forms
How to Make a Before_Save Conditional
Are There Any Ruby Orms Which Use Cursors or Smart Fetch
How to Test Instance Variable Was Instantiated in Controller with Rspec