3

Given that there is require_relative, which accepts relative as well as absolute paths, is there any reason to particularly use require, which accepts only absolute paths? Can all require be replaced by require_relative?

Edit

Sergio Tulentsev noticed me that require_relative cannot be used for gems. Why is it designed so? What problem would arise if require_relative were merely a superset of require and require be deprecated? Or, is it just because require_relative appeared later than the incorporation of gems into the Ruby core system, and the author of require_relative did not do modification to the code around the gems system?

sawa
  • 165,429
  • 45
  • 277
  • 381

3 Answers3

6
require 'json'

require can be used also to require code from gems and require_relative can't do that. Ergo, the latter can not be a replacement for the former.

RE: edit

I think the answer to your additional question is: code complexity. require has additional power to search lib path. require_relative substitutes base path and so on. Unifying all features in the same function would probably overcomplicate it. The more complex code is, the more likely it is to have bugs. That's my guess.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
1

Can all require be replaced by require_relative?

No: it's the other way around: require_relative is a convenient subset of require:

require_relative('path')

equals:

require(File.expand_path('path', File.dirname(__FILE__)))

if __FILE__ is defined, or it raises LoadError otherwise.

is there any reason to particularly use require?

You can only do the following operations with require and not require_relative:

  • requires relative to the current directory (not current file): require './a.rb'

  • uses the search path ($LOAD_PATH) to require: require 'a.rb'

    This is not possible with require_relative because the docs say that path search only happens when "the filename does not resolve to an absolute path" (i.e. starts with / or ./ or ../), which is always the case for File.expand_path.


I have explained this in more detail at: https://stackoverflow.com/a/26565471/895245

Community
  • 1
  • 1
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
0

Ruby has the -I flag for including directories for the purpose of require lookup. So if you either push the folder to the $: variable (which is to say ENV['PATH']) or use the Isome_lib at the command line then require can be used without specifying the base folder location for that file.

require_relative is used for a different purpose, which is to use the __FILE__ directory location to refer to location for the file you are wanting to require. So it could be used to require files from a gem, but it could be more work to do so. Realizing that gems can be 'vendored' and located in a directory that is very close, or even contained, by your application. Normally, gems that are installed are placed into the $PATH for ease of lookup, so this is not normally needed.

Requiring gems using require_relative is an anti-pattern, though, as it should probably be required as a gem in your Gemfile where you can specify the source of the file and the environment will be loaded for the application using Bundler, perhaps.

vgoff
  • 10,980
  • 3
  • 38
  • 56