require ‘rubygems’ isn’t (necessarily) wrong

Ryan Tomayko is a smart guy and a solid coder. But with all due props, he’s wrong about require 'rubygems' always being wrong (h/t RubyInside).

Okay, not actually wrong. I see his point. In order to be able to plug different components together easily, those components shouldn’t care how they’re included, nor specify exactly how they include the other components they depend on.

But here’s the problem. For command-line applications or other executables, following Ryan’s advice simplistically leads to crap like this:

#!/usr/bin/ruby -W0

# I don't believe in requiring rubygems itself
#require 'rubygems'
require 'highline'
require 'rbosa'
require 'ShortURL'
gem('twitter4r', '0.3.0')
require 'time'
require 'twitter'
        <span id="more-994"></span>

        <p>This anonymous coder posted a script to a general <a href="http://www.macosxhints.com/">Mac OS X tips site</a> which not only uses a bunch of things distributed primarily as gems, but also <em>directly calls</em> <code>gem()</code>.</p>

The person who posted this hint should’ve done one of two things:

  1. Changed the shebang line to include rubygems, like so:

    #!/usr/bin/env ruby -W0 -rubygems

    …and/or included instructions to change the shebang line to fit one’s own environment.

  2. Left out the shebang line in the script, and included instructions for running it in his post:

    $ ruby -rubygems safaweet.rb

    (tee hee, sa-FA-weet. I just came up with that. Say it out loud. You’ll laugh.)

There’s basically nothing wrong with defaulting to using RubyGems, so long as you make it easy enough to modify the software not to (for which Ryan Tomayko’s three points of advice are actually useful). Perl hackers assume you use CPAN; Python hackers assume you have easy_install. And modern Unices that include Perl, Python, and Ruby, also include CPAN, easy_install, and RubyGems; thus we can reasonably assume now that Ruby users have RubyGems.


About this entry