Issue #8781 has been updated by drbrain (Eric Hodel).


Following an IRC conversation with Koichi I don't believe this bug proposes to eliminate require_relative outright, instead it is for investigating if require_relative should be used more in the standard library and in gems. The optimization is useful for libraries with many files as it eliminates repeated $LOAD_PATH lookups.

Regarding the $LOAD_PATH trick restriction of require_relative:

For a gem it is not overly restrictive. I typically replace whole libraries as it is easier and more convenient that replacing just one file.

1) gem unpack foo or git clone git / ...:foo
2) edit files
3) ruby -I foo/lib ???

Is easier than

1) mkdir new/path/to/dir
2) cp {old,new}/path/to/dir/foo.rb
3) edit file
4) ruby -Ilib ???

For the second case, a need to edit multiple files and possibly create a patch makes the first option much more convenient..

For files in the standard library, replacing a file loaded by require_relative that is not part of a gem is more difficult. To alter net/http/request.rb loaded by require_relative you must duplicate the tree of files that require_relative it in order to use the $LOAD_PATH trick. I see adding features of the standard library as default gems a workaround for this restriction.

How is require_relative more brittle that require? If a file is removed from a gem it can be loaded from the wrong path (via -I if in a gem or vice versa). Using require_relative the error is immediate and obvious. It seems to eliminate this class of error entirely.

I would like to note that autoload can't benefit from this optimization as it always uses $LOAD_PATH. Koichi suggested adding a relative: true keyword argument to support this.
----------------------------------------
Feature #8781: Use require_relative() instead of require() if possible
https://bugs.ruby-lang.org/issues/8781#change-41117

Author: ko1 (Koichi Sasada)
Status: Open
Priority: Normal
Assignee: 
Category: lib
Target version: current: 2.1.0


I wrote a attached small script rrc.rb, stand for "RequireRelativeChecker".

This small script points out that require() can be replaced with require_relative().

"Detecting replace-able require()" algorithm is easy (and not perfect):
(1) If loaded file is at sub (or same) directory of requiring file.
(2) If requiring file foo.rb is at $LOAD_PATH, then check only foo/*.
See attached script for details.

This is a part of output.

####
/home/ko1/tmp/trunk/lib/ruby/2.1.0/cgi.rb:294: WARNING: Use require_relative() to require /home/ko1/tmp/trunk/lib/ruby/2.1.0/cgi/core.rb.
/home/ko1/tmp/trunk/lib/ruby/2.1.0/cgi.rb:295: WARNING: Use require_relative() to require /home/ko1/tmp/trunk/lib/ruby/2.1.0/cgi/cookie.rb.
/home/ko1/tmp/trunk/lib/ruby/2.1.0/date.rb:4: WARNING: Use require_relative() to require /home/ko1/tmp/trunk/lib/ruby/2.1.0/date/format.rb.
/home/ko1/tmp/trunk/lib/ruby/2.1.0/net/http.rb:1541: WARNING: Use require_relative() to require /home/ko1/tmp/trunk/lib/ruby/2.1.0/net/http/exceptions.rb.
/home/ko1/tmp/trunk/lib/ruby/2.1.0/net/http.rb:1543: WARNING: Use require_relative() to require /home/ko1/tmp/trunk/lib/ruby/2.1.0/net/http/header.rb.
/home/ko1/tmp/trunk/lib/ruby/2.1.0/net/http.rb:1545: WARNING: Use require_relative() to require /home/ko1/tmp/trunk/lib/ruby/2.1.0/net/http/generic_request.rb.
###

(all of warnings are attached)

How about to replace require() with require_relative() if it is possible?

Advantage:
* require_relative() is faster than require() especially with many gems.
* Easy to detect which file is loaded.

Disadvantage (incompatibility)
* We can't replace loading file with $LOAD_PATH trick.
(But I believe nobody expect such behavior)

(I also recommend other gem authors to  use require_relative)

Any comments?



-- 
http://bugs.ruby-lang.org/