ruby picture

RCR 341: Let Kernel#require take multiple arguments

Submitted by rixxon (Thu Jul 13 01:30:08 UTC 2006)

Abstract

Let Kernel#require try to load all the specified libraries.

Problem

Kernel#require can only load one library per call. So either we write several require calls,

  require 'foo'
  require 'bar'
  require 'xyzzy'

or iterate an array of libraries (doesn't work too well with rdoc),

  %w[foo bar xyzzy].each {|lib| require lib }

Although this works fine, it feels natural that require should be able to take multiple arguments. In Python for example, it is perfectly valid to

  import foo, bar, xyzzy

Proposal

My proposal is simple:

  require 'foo', 'bar', 'xyzzy'

which will also make this possible,

  require %w[foo bar xyzzy]

I think this leads to cleaner syntax; you can load multiple libraries on a single line without resorting to iterators.

Analysis

Would not break backwards compatibility.

Implementation

  module Kernel
    alias :old_require :require
    def require(*libs)
      libs.each {|lib| old_require lib }
    end
  end
ruby picture
Comments Current voting
Strongly opposed 0
Opposed 0
Neutral 0
In favor 0
Strongly advocate 0
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .