Project

General

Profile

Actions

Feature #8834

open

Kernel#load_relative

Added by sawa (Tsuyoshi Sawada) over 11 years ago. Updated over 5 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:56866]

Description

The intended difference between Kernel#require and Kernel#load is that the former is for external libraries and the latter is for Ruby scripts internal to the project. Considering this fact, load should be more likely than require to be used in a situation where you want to call a file through a relative path. Strangely, there is Kernel#require_relative, but no Kernel#load_relative. I request Kernel#load_relative. It is even more necessary than Kernel#require_relative.

It seems to me that people are using Kernel#require_relative when they want to use a relative path, even in the context where they are supposed to use load because of the lack of Kernel#load_relative. I don't think this is a good practice. Furthermore, in cases where you have a file without a .rb or other extention that you want to call via a relative path, there is no good way to do it.

Updated by phluid61 (Matthew Kerwin) over 11 years ago

This is an unusual description of the "intended difference" between the methods. I understood the difference between the two was: #require loads the file only once, #load does it every time. Yes, this implies that #load is for project-relative files (e.g. loading data), but it doesn't mean that #require is only for external libraries.

Updated by sawa (Tsuyoshi Sawada) over 11 years ago

phluid61

I think what you wrote is the other way around. The only-once feature is due to the division of labor, not the other way around. See this:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/21562

Updated by phluid61 (Matthew Kerwin) over 11 years ago

sawa (Tsuyoshi Sawada) wrote:

phluid61

I think what you wrote is the other way around. The only-once feature is due to the division of labor, not the other way around. See this:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/21562

I understand. I had a slightly different definition of "script" in my head. I'd forgotten that require doesn't need an extension (and can thus load any .rb, .so, etc. file), where load requires the extension.

In that vein, I personally have never seen anyone use require/_relative when they should have used load. I've never seen a project that has "scripts" that aren't library code, even if they're only project-specific libraries.

Note: I'm not arguing against #load_relative exactly, I've just never seen a need. Maybe its existence, and an education campaign, can make it useful.

Updated by Anonymous over 11 years ago

On 08/29/2013 07:27 PM, sawa (Tsuyoshi Sawada) wrote:

It seems to me that people are using Kernel#require_relative when
they want to use a relative path, even in the context where they are
supposed to use load because of the lack of Kernel#load_relative.

Kernel#load does try to load relatively first, doesn't it? Quoting ri:

If the filename does not resolve to an absolute path, the file is
searched for in the library directories listed in $:.

What would Kernel#load_relative do?

(I do wish the wrap param of #load could be a module to use as the
wrapper.)

Updated by nobu (Nobuyoshi Nakada) over 11 years ago

(13/08/30 12:53), Joel VanderWerf wrote:

On 08/29/2013 07:27 PM, sawa (Tsuyoshi Sawada) wrote:

It seems to me that people are using Kernel#require_relative when
they want to use a relative path, even in the context where they are
supposed to use load because of the lack of Kernel#load_relative.

Kernel#load does try to load relatively first, doesn't it? Quoting ri:

If the filename does not resolve to an absolute path, the file is
searched for in the library directories listed in $:.

What would Kernel#load_relative do?

It's relative to the cwd, whereas require_relative does
relatively to the script path calling it.

Updated by TylerRick (Tyler Rick) over 5 years ago

This seems like a curious omission. It would be useful to have a load_relative that loads relative to the script path calling it, like require_relative.

I guess the workaround is to use __dir__?

load File.join(__dir__, 'other.rb')
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0