Project

General

Profile

Actions

Feature #7121

open

Extending the use of `require'

Added by mjones (Morgan Jones) about 12 years ago. Updated 8 months ago.

Status:
Assigned
Target version:
-
[ruby-core:47869]

Description

I was playing with Ruby tonight and thought up an interesting idea to make (({require})) a bit better, so you can load multiple files sequentially using one method call.

Currently, (({require})) supports one argument, and throws a (({TypeError})) if you pass an array:

 irb(main):001:0> require %w(json yaml)
 TypeError: can't convert Array into String

However, there's a way to patch Kernel that makes it respond to multiple objects passed in an (({Array})).

 module Kernel
   @@require = method :require

   def require *args
     args.flatten!
     args.collect! do |a|
       raise ArgumentError.new "arguments to `require' must be strings or symbols" unless a.is_a?(String) || a.is_a?(Symbol)
       @@require.call a.to_s
     end

     args.length == 1 ? args.first : args
   end
 end

The new behavior doesn't actually require the modification of any code that calls (({require})) (pretty much anything, really), and new code can take advantage of the new functionality instantly.

 irb> require %w(json yaml)
 => [true, false]
 irb> require :pp
 => false
 irb> require 'rails'
 => true
 irb> require %w(json yaml), :pp, 'rails'
 => [true, false, false, true]

Thanks for considering this.


Related issues 2 (2 open0 closed)

Related to Ruby master - Feature #14927: Loading multiple files at onceAssignedmatz (Yukihiro Matsumoto)Actions
Related to Ruby master - Feature #12858: Supporting batch-requiring of files in rubyOpenActions

Updated by wardrop (Tom Wardrop) about 12 years ago

I personally don't mind your suggestion. It makes sense to me. I can't think of any potential negative side effects. On that note, I think the return value should always be a boolean, instead of a boolean OR an array of booleans depending on the input. #require should return true if any of the listed files were loaded, or false if none of them were. If you need to determine the loaded state of each file (the lesser common use case I'd imagine), then you should resort to looping over multiple calls to #require.

Updated by mame (Yusuke Endoh) about 12 years ago

  • Status changed from Open to Assigned
  • Assignee set to matz (Yukihiro Matsumoto)
  • Target version set to 2.6

Updated by trans (Thomas Sawyer) about 12 years ago

It's ugly, as it makes code harder to read. Please no.

Updated by prijutme4ty (Ilya Vorontsov) almost 12 years ago

Just an alternative idea. What about using Regexp as an alternative to String?
require /.*_helper/

Actions #5

Updated by naruse (Yui NARUSE) almost 7 years ago

  • Target version deleted (2.6)
Actions #6

Updated by hsbt (Hiroshi SHIBATA) 8 months ago

  • Description updated (diff)
Actions #7

Updated by hsbt (Hiroshi SHIBATA) 8 months ago

Actions #8

Updated by hsbt (Hiroshi SHIBATA) 8 months ago

  • Related to Feature #12858: Supporting batch-requiring of files in ruby added
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0