Difference Between Require_Relative and Require in Ruby

What is the difference between require_relative and require in Ruby?

Just look at the docs:

require_relative complements the builtin method require by allowing you to load a file that is relative to the file containing the require_relative statement.

For example, if you have unit test classes in the "test" directory, and data for them under the test "test/data" directory, then you might use a line like this in a test case:

require_relative "data/customer_data_1"

Understanding the difference between `load`, `require`, and `require_relative`

load is used when you want to import a file irrespective of whether it has been already imported. require or require_relative is used when you want to import a file only if it has not been already.

From this, it follows that the former is used when the imported file is the object of analysis (data file), whereas the latter is used to provide some features to be used in the program (part of the program, library, framework).

While require can only handle paths relative to $:, require_relative is an extension that can handle paths relative to current directory as well. require_relative is a superset of require, and require can be dispensed (although require_relative is written using require, so it has to be rewritten if require is to be dispensed).

Can `require` be replaced by `require_relative`?

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.

What is the difference between include and require in Ruby?

What's the difference between
"include" and "require" in Ruby?

Answer:

The include and require methods do
very different things.

The require method does what include
does in most other programming
languages: run another file. It also
tracks what you've required in the
past and won't require the same file
twice. To run another file without
this added functionality, you can use
the load method.

The include method takes all the
methods from another module and
includes them into the current module.
This is a language-level thing as
opposed to a file-level thing as with
require. The include method is the
primary way to "extend" classes with
other modules (usually referred to as
mix-ins). For example, if your class
defines the method "each", you can
include the mixin module Enumerable
and it can act as a collection. This
can be confusing as the include verb
is used very differently in other
languages.

Source

So if you just want to use a module, rather than extend it or do a mix-in, then you'll want to use require.

Oddly enough, Ruby's require is analogous to C's include, while Ruby's include is almost nothing like C's include.

How does load/require/require_relative handle a file with no file extension?

From the documentation:

Ruby tries adding “.rb”, “.so”, and so on to the name until found.

http://apidock.com/ruby/Kernel/require

What is the difference between require and load?

If you require the same file twice, it will be loaded and evaluated only once. load, on the other hand, loads and evaluates the file every time. There are also differences in how actual filename is resolved (thanks, Saurabh).

What does this mean practically?

Let's say we have a library foo

# foo.rb

class Foo
def bar
puts 'bar'
end

def quux
puts 'quux'
end
end

Then we have a file which makes some non-idempotent operations. Say, undefines a method

# mod.rb
class Foo
undef :bar
end

Then, if we require mod.rb twice, nothing bad happens. bar gets successfully undefined.

# main.rb
require './foo'

Foo.instance_methods(false) # => [:bar, :quux]

require './mod'
require './mod'

Foo.instance_methods(false) # => [:quux]


But if we load mod.rb twice, then second undef operation will fail, because method is already gone:

# main.rb
require './foo'

Foo.instance_methods(false) # => [:bar, :quux]

load 'mod.rb'
load 'mod.rb'

Foo.instance_methods(false) # =>
# ~> mod.rb:2:in `<class:Foo>': undefined method `bar' for class `Foo' (NameError)
# ~> from mod.rb:1:in `<top (required)>'
# ~> from -:6:in `load'
# ~> from -:6:in `<main>'

There's no error with require because in that case undef happens only once. Granted, this example is quite contrived, but I hope it illustrates the point.



Related Topics



Leave a reply



Submit