Project

General

Profile

Actions

Feature #19006

closed

Inconsistent behaviour of autoload in wrapped script

Added by shioyama (Chris Salzberg) over 1 year ago. Updated over 1 year ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:109924]

Description

Suppose I have two files, foo.rb and bar.rb:

# foo.rb

puts "loading Foo..."

module Foo
  autoload :Bar, "foo/bar"
end

and

# foo/bar.rb

puts "loading Foo::Bar..."

module Foo
  module Bar
  end
end

I can require "foo" and access both Foo and Foo::Bar:

require "foo"
# loading Foo...
#=> true
Foo::Bar
# loading Foo::Bar...
#=> Foo::Bar

However, if I load foo under a wrap module with load:

MyModule = Module.new
load "./foo.rb", MyModule
# loading Foo...
#=> true

... I'm now unable to access Foo::Bar anywhere, because whereas the constant is autoloaded from MyModule::Foo::Bar, it is required from the top-level as Foo::Bar:

MyModule::Foo::Bar
# loading Foo::Bar
#=> uninitialized constant MyModule::Foo::Bar (NameError)

This means that autoload is basically useless inside anything loaded with the wrap argument to load, because the file being autoloaded can't know in advance what the base namespace will be.

I would argue that it makes much more sense to apply the wrap module (top_wrapper) to any autoloaded file loaded when top_wrapper is set. In the example above, this would mean that accessing MyModule::Foo::Bar would work, since MyModule would apply when the autoload triggers to load foo/bar.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0