Module: Kernel

Defined in:
lib/zeitwerk/kernel.rb

Class Method Summary collapse

Class Method Details

.require(path) ⇒ Boolean

Parameters:

  • path (String)

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/zeitwerk/kernel.rb', line 24

def require(path)
  if loader = Zeitwerk::Registry.loader_for(path)
    if path.end_with?(".rb")
      zeitwerk_original_require(path).tap do |required|
        loader.on_file_autoloaded(path) if required
      end
    else
      loader.on_dir_autoloaded(path)
    end
  else
    zeitwerk_original_require(path).tap do |required|
      if required
        realpath = $LOADED_FEATURES.last
        if loader = Zeitwerk::Registry.loader_for(realpath)
          loader.on_file_autoloaded(realpath)
        end
      end
    end
  end
end

.zeitwerk_original_requireObject

We are going to decorate Kerner#require with two goals.

First, by intercepting Kernel#require calls, we are able to autovivify modules on required directories, and also do internal housekeeping when managed files are loaded.

On the other hand, if you publish a new version of a gem that is now managed by Zeitwerk, client code can reference directly your classes and modules and should not require anything. But if someone has legacy require calls around, they will work as expected, and in a compatible way.

We cannot decorate with prepend + super because Kernel has already been included in Object, and changes in ancestors don’t get propagated into already existing ancestor chains.



20
# File 'lib/zeitwerk/kernel.rb', line 20

alias_method :zeitwerk_original_require, :require