Constant access in a nested module
source link: https://www.codesd.com/item/constant-access-in-a-nested-module.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Constant access in a nested module
I'm having trouble accessing a module constant in a nested module. Here's the code:
outer.rb
require 'inner.rb'
module Outer
BASE_DIR = "cache/"
end
inner.rb
module Outer
module Inner
puts BASE_DIR
end
end
If I run the code in inner.rb I get the following error:
<module:Inner>': uninitialized constant Outer::Inner::BASE_DIR (NameError)
I thought that since BASE_DIR is declared in the outer module is should also be accessible in the inner module and it does not seem to be the case.
It is a question of load order. Replacing the require with the actual code required reveals that your code is loaded in this order:
module Outer
module Inner
puts BASE_DIR
end
end
module Outer
BASE_DIR = "cache/"
end
Now it is pretty easy to see why that cannot work. As the error message suggests, the constant simply is not defined at the time when you try to access it. This happens because every piece of code that is not inside a method definition will be executed immediately. Accessing the constant from a method is however possible:
module Outer
module Inner
def self.foo
puts BASE_DIR
end
end
end
module Outer
BASE_DIR = "cache/"
end
Outer::Inner.foo
# cache/
There are several possible solutions, depending on your needs:
- eliminate the use of the constant outside methods (may not be an option)
- change load order (put the
require
at the end of the file) - Delegate the storage of global settings to a dedicated class/module
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK