Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Why "require 'rubygems'" In Your Library/App/Tests Is Wrong
In response to all the responses to:
http://twitter.com/rtomayko/status/1155906157
You should never do this in a source file included with your library,
app, or tests:
require 'rubygems'
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.
When you feel the urge to "require 'rubygems'" because some shit isn't
working, stop and consider whether one of these solutions is more
appropriate:
1. RubyGems installs special versions of executables included with
gems. Here's rake's as an example:
$ cat /opt/local/bin/rake
#!/opt/local/bin/ruby
#
# This file was generated by RubyGems.
#
# The application 'rake' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'rubygems'
version = ">= 0"
if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then
version = $1
ARGV.shift
end
gem 'rake', version
load 'rake'
In this case, rubygems is required when the executable is run. Explicitly
requiring rubygems in code loaded by these files doesn't make sense. The
whole point of these wrapper scripts is to push the rubygems loading
machinery into the environment and out of your app/library/tests.
2. When running "ruby FOO.rb" and a LoadError occurs because rubygems
is not available, DO NOT add "require 'rubygems'" to FOO.rb. Instead,
run the command as "ruby -rubygems FOO.rb". This will require rubygems
before evaluating FOO.rb.
3. When running "ruby FOO.rb" and a LoadError occurs because rubygems
is not available, DO NOT add "require 'rubygems'" to FOO.rb. Instead,
set the RUBYOPT environment variable:
$ RUBYOPT="rubygems"
$ export RUBYOPT
$ ruby FOO.rb
You can even put that in your ~/.{bash,zsh,sh}rc if you prefer to always
have rubygems loaded and available.
Why You Shouldn't Force Rubygems On People
------------------------------------------
When I use your library, deploy your app, or run your tests I may not want
to use rubygems. When you "require 'rubygems'" in your code, you remove my
ability to make that decision. I cannot unrequire rubygems, but you can
not require it in the first place.

sferik commented Nov 15, 2010

Because your library's users might prefer to use a different package manager, like rip or dpkg.

Granted, I'm new to ruby, But it seems like using the RUBYOPT variable with "rubygems" I get : ruby: no such file to load -- ubygems (LoadError). That's at the very least confusing.

amit352 commented Apr 3, 2015

I am new to ruby, but as per http://guides.rubygems.org/rubygems-basics/#requiring-code, it says must use require 'rubygems' for Ruby 1.8, I come to this page while I was trying to make a skeleton for rack-ember application.
Also it will be good if you can provide me any link for rack application skeleton.

Ajedi32 commented Jun 24, 2016

@johnlabarge Yeah, -rubygems is incorrect. It should be -rrubygems. The first r is short for "require". See ruby --help:

-rlibrary       require the library before executing your script

fidelisrafael commented Jul 23, 2016 edited

@Ajedi32 That's not really true, there's a ubygem file, so -rubygems works as expected.

If you need to remove this from a bunch of source files, this library might help 😛

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment