From: "trans (Thomas Sawyer)" Date: 2013-08-12T23:16:27+09:00 Subject: [ruby-core:56577] [ruby-trunk - Feature #8781] Use require_relative() instead of require() if possible Issue #8781 has been updated by trans (Thomas Sawyer). require_relative makes sense for "packages", i.e. where you have a main file that loads a bunch of subordinate files. In other words, the subordinate files could have all been in the main file, but are split out for better organization. So the brittleness you speak of is not an issue here. A much worse brittleness that require_relative helps prevent is when two libraries clobber each other by using the same absolute library path --it shouldn't happen in a well structured project, but people don't always follow the proper conventions. ---------------------------------------- Feature #8781: Use require_relative() instead of require() if possible https://bugs.ruby-lang.org/issues/8781#change-41112 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/