I'm not sure I've seen this addressed, but I am wondering what is the best way to do multiple requires in a ruby script. I have come up with a couple rudimentary examples which I will outline below, but I'm not sure if there is a best practice for this -- my search results have come back with nothing.
0) Bunch of includes & exceptions (I'll leave the rescue out)
require 'rubygems' require 'builder'
1) String array
torequire = ['rubygems', 'builder'] begin torequire.each do |req| require req rescue LoadError => e # Not sure if this is great either puts "Missing required gem: " + e.message.split[-1] exit end
Is there a large problem created from loading them all from a string array? You could specify version requirements or locations similarly, I'm just wondering if there is a problem with doing it this way.
The plain way is the best way.
You could do this, but it trades clarity for cleverness--a poor bargain:
[ 'rubygems', 'rack', 'rails' ].each(&method(:require))
Skip the "rescue" with the fancy error message. Everyone knows what it means when a require throws a stack trace.
If you want to make it easier for someone using your program to have the required gems installed, check out bundler.