Yes. require doesn’t “insert” code there. require only loads a
library, i.e., reads and evaluates that always in the top level
context unless already loaded.
A Ruby code has identical semantics regardless of require’d or
directly invoked:
% cat ./usee.rb
def a(arg)
[FILE, arg, self]
end
p [1, a(FILE)]
% ruby ./usee.rb
[1, [“./usee.rb”, “./usee.rb”, main]]
% cat ./user.rb
class C
require “usee”
p [2, a(FILE)]
end
require “usee”
p [3, a(FILE)]
p [4, C.public_methods.include?(“a”)]
p [5, Object.private_methods.include?(“a”)]
% ruby ./user.rb
[1, [“./usee.rb”, “./usee.rb”, main]]
[2, [“./usee.rb”, “./user.rb”, C]]
[3, [“./usee.rb”, “./user.rb”, main]]
[4, false]
[5, true]
[5] because a method defined in top level is a function, i.e., a
private method of Object, that is, could not be called with explict
receiver.
In other word, Ruby’s require is similar to Emacs-Lisp’s require,
which never loads a library twice and doesn’t change the meaning of a
library to be loaded.
– Gotoken
···
At Sat, 3 Aug 2002 03:14:44 +0900, bbense+comp.lang.ruby.Aug.02.02@telemark.stanford.edu wrote:
[1] and [2] have the same effect for ruby interpreter at all because
namespace will not be nested by require' or
load’.
-
- So require is not just a “smart” include, but something that
works at the language level[1].