[#56333] [CommonRuby - Feature #8723][Open] Array.any? predicate returns true for empty array. — "nurettin (Nurettin Onur TUGCU)" <onurtugcu@...>

12 messages 2013/08/02

[#56368] [ruby-trunk - Bug #8730][Open] "rescue Exception" rescues Timeout::ExitException — "takiuchi (Genki Takiuchi)" <genki@...21g.com>

15 messages 2013/08/04

[#56407] [ruby-trunk - misc #8741][Open] email notification on bugs.ruby-lang.org is broken — "rits (First Last)" <redmine@...>

18 messages 2013/08/05

[#56524] [ruby-trunk - Bug #8770][Open] [PATCH] process.c: avoid EINTR from Process.spawn — "normalperson (Eric Wong)" <normalperson@...>

19 messages 2013/08/10

[#56536] [ruby-trunk - Feature #8772][Open] Hash alias #| merge, and the case for Hash and Array polymorphism — "trans (Thomas Sawyer)" <redmine@...>

24 messages 2013/08/11

[#56544] [ruby-trunk - Bug #8774][Open] rb_file_dirname return wrong encoding string when dir is "." — jiayp@... (贾 延平) <jiayp@...>

10 messages 2013/08/11

[#56569] [ruby-trunk - Feature #8781][Open] Use require_relative() instead of require() if possible — "ko1 (Koichi Sasada)" <redmine@...>

31 messages 2013/08/12
[#56582] [ruby-trunk - Feature #8781] Use require_relative() instead of require() if possible — "drbrain (Eric Hodel)" <drbrain@...7.net> 2013/08/12

[#56584] Re: [ruby-trunk - Feature #8781] Use require_relative() instead of require() if possible — SASADA Koichi <ko1@...> 2013/08/12

(2013/08/13 2:25), drbrain (Eric Hodel) wrote:

[#56636] Re: [ruby-trunk - Feature #8781] Use require_relative() instead of require() if possible — Aaron Patterson <tenderlove@...> 2013/08/16

On Tue, Aug 13, 2013 at 07:38:01AM +0900, SASADA Koichi wrote:

[#56634] [ruby-trunk - Feature #8788][Open] use eventfd on newer Linux instead of pipe for timer thread — "normalperson (Eric Wong)" <normalperson@...>

11 messages 2013/08/16

[#56648] [ruby-trunk - Bug #8795][Open] "Null byte in string error" on Marshal.load — "mml (McClain Looney)" <m@...>

17 messages 2013/08/16

[#56824] [ruby-trunk - Feature #8823][Open] Run trap handler in an independent thread called "Signal thread" — "ko1 (Koichi Sasada)" <redmine@...>

14 messages 2013/08/27

[#56878] [ruby-trunk - misc #8835][Open] Introducing a semantic versioning scheme and branching policy — "knu (Akinori MUSHA)" <knu@...>

11 messages 2013/08/30

[#56890] [ruby-trunk - Feature #8839][Open] Class and module should return the class or module that was opened — "headius (Charles Nutter)" <headius@...>

26 messages 2013/08/30

[#56894] [ruby-trunk - Feature #8840][Open] Yielder#state — "marcandre (Marc-Andre Lafortune)" <ruby-core@...>

14 messages 2013/08/30

[ruby-core:56684] [ruby-trunk - Feature #8781] Use require_relative() instead of require() if possible

From: "trans (Thomas Sawyer)" <redmine@...>
Date: 2013-08-17 11:51:06 UTC
List: ruby-core #56684
Issue #8781 has been updated by trans (Thomas Sawyer).


But it doesn't matter *if* it is downward, b/c it is relative to the internal structure. If I move something around in my internal structure, that I might have alter a require statement is to be expected. In your example, if we move `foo/bar/baz` we'll still break a require, regardless of where foo.rb resides. Worse still, if an external program had depended on the location of foo.rb, moving foo.rb would break their program. In practice, the foo.rb of your example rarely moves, by its very design. It represents a component and the things under `foo/` are its subcomponents. Some code in the file might move but not the file itself.

I feel like you are making something of a strawman argument against using require_relative b/c it is doing exactly what require_relative is supposed to do. Meanwhile ignoring its benefits. I really don't understand the objection to it. Used judiciously (better than "properly"?) it works quite well.

As for "proper" use, two dollars to a donut most of those uses of `../` relative requires will be in test scripts. I don't even have to look. That's okay b/c it's localized to building the project. I'd still recommend removing it, for example adding `test` directory to the $LOAD_PATH when running tests. The usage I am talking about is within lib where we are structuring our programs from top level structures to low level structures. It's just the common pattern that we've all used:

  module MyApp       -> myapp/
    class Foo        -> foo.rb and foo/
      class Bar      -> foo/bar.rb


So in foo.rb we require 'foo/bar.rb' not the other way around. That's all I mean by "proper".

Hey, let me make one other interesting point. I would be totally supportive of not using require_relative --requiring relative to the current file, if we could require relative to the current library's base directory. The load system I created actually has this feature. I think it is really the most "proper" solution of all, but it requires that Ruby have some notion of a library as having a base directory. (To clarify, in your example code `lib/foo/` is the base directory of the library.)

----------------------------------------
Feature #8781: Use require_relative() instead of require() if possible
https://bugs.ruby-lang.org/issues/8781#change-41216

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/

In This Thread