Feature #7611

Focal method for all loads/requires

Added by Thomas Sawyer almost 3 years ago. Updated over 2 years ago.

Assignee:Yukihiro Matsumoto


Presently Ruby has seven methods for importing code. These are:

  • (({Kernel#load}))
  • (({Kernel#require}))
  • (({Kernel#relative_require}))
  • (({Kernel.load}))
  • (({Kernel.require}))
  • (({Kernel.relative_require}))
  • (({Kernel#autoload}))

Even though the Kernel module methods do the same thing as the instance methods, all of these act independently. If you need to tap into or override code loading in general it means doing so for each and every one.

Would it not be much more elegant if they all called upon one focal method? Lacking another name for this example, lets call it (({#open_eval()})). The method would take a path argument and options for (({feature})), (({relative})) and (({wrap})), where if (({feature})) is true then it is a "require", otherwise it is a "load".

def open_eval(path, feature: false, relative: false, wrap: nil)

All the other seven methods listed above would route to this one method. So it would then be possible to monitor or override this behavior, e.g. like RubyGems does, via a single interface.

Note, even if this feature request is not accepted, I would like to get some opinion on it, b/c I am currently working on a project where I have to do this (specifically I am in need of require/load callbacks). For the purpose I have created another reusable gem for it and I want to get an idea of what would be considered appropriate API for it.


#1 Updated by Zachary Scott almost 3 years ago

IRB adds even more requires, but I digress.

#2 Updated by Koichi Sasada over 2 years ago

  • Assignee set to Yukihiro Matsumoto

Also available in: Atom PDF