Skip to content

Instantly share code, notes, and snippets.

@shuhei
Last active August 29, 2015 14:17
Show Gist options
  • Save shuhei/05ed0e96718151903c4b to your computer and use it in GitHub Desktop.
Save shuhei/05ed0e96718151903c4b to your computer and use it in GitHub Desktop.
Requiring 'active_support/dependencies' changes how constant is resolved

Requiring 'active_support/dependencies' changes how constant is resolved

Without 'active_support/dependencies'

$ ruby constant_resolution.rb
inner - Hello
[Outer::Inner, Outer]
constant_resolution.rb:14:in `const_missing': uninitialized constant Outer::Inner::Hello (NameError)
	from constant_resolution.rb:19:in `<main>'

With 'active_support/dependencies'

$ ruby constant_resolution.rb
inner - Hello
[Outer::Inner, Outer]
outer - Hello
[Outer]
# require 'active_support/dependencies'
module Outer
def self.const_missing(const_name)
puts "outer - #{const_name}"
puts Module.nesting.inspect
'outer'
end
module Inner
def self.const_missing(const_name)
puts "inner - #{const_name}"
puts Module.nesting.inspect
super
end
end
end
Outer::Inner::Hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment