Consider the following (correct) Ruby program:
class Outer
Inner = Struct.new(:dummy) do
CONST = \'abce\'
def fun
puts(dummy)
end
end
This happens because the constant is defined in the current namespace. The class
and module
keywords define namespaces, but Struct.new
(just like Class.new
) does not.
In order to define the constant under the Struct's scope, you have to use self::
class Outer
Inner = Struct.new(:dummy) do
self::CONST = 'abce'
end
end
Outer::Inner::CONST
#=> 'abce'
Outer::CONST
#=> NameError uninitialized constant Outer::CONST