Why Using require ‘rubygems’ Is Wrong

By Peter Cooper

Ryan Tomayko, currently known as one of the lead developers of Sinatra, was definitely not mincing his words yesterday when he posted Why "require 'rubygems'" In Your Library/App/Tests Is Wrong:

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.

But.. why?

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.

I hadn't thought of it this way before, but Ryan makes a lot of sense. The choice of library management system should be down to the environment and not enforced by a library or a single portion of a larger application.

Check out Ryan's post for the deeper rationale and see what you think. He's definitely got me thinking twice about using require 'rubygems' in my own scripts, although as the slow migration over to Ruby 1.9 begins, perhaps it will become a non-issue, since RubyGems is included with Ruby 1.9 and loaded by default. Thoughts?