From: "Eregon (Benoit Daloze)" Date: 2013-08-13T18:53:46+09:00 Subject: [ruby-core:56595] [ruby-trunk - Feature #8781] Use require_relative() instead of require() if possible Issue #8781 has been updated by Eregon (Benoit Daloze). ko1 (Koichi Sasada) wrote: > (2013/08/13 2:25), drbrain (Eric Hodel) wrote: > > 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. > > I think this proposal depends on that how many people want to do such a > replacement. I doubt many users do this. For my part I always track the project under some version control and edit the file directly (except for the smallest change when the editor undo feature is enough). Making the structure is probably even easier with `cp -R lib/net edited` than `mkdir -p edited/net/http && cp lib/net/http/request.rb edited/net/http`. ---------------------------------------- Feature #8781: Use require_relative() instead of require() if possible https://bugs.ruby-lang.org/issues/8781#change-41131 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/