Given the following Ruby class
class Example
PARENTS = [
FATHER = :father,
MOTHER = :mother
]
end
These work as expected
> Example::PARENTS
#=> [:father, :mother]
> Example::PARENTS[0]
#=> :father
> Example::PARENTS[1]
#=> :mother
However, why does this work?
> Example::FATHER
#=> :father
> Example::MOTHER
#=> :mother
In fact, why are there three constants in the Example class's scope?
> Example.constants
#=> [:MOTHER, :PARENTS, :FATHER]
To the point, that if I extend the class with an additional method:
class Example
def self.whos_your_daddy
FATHER
end
end
It accesses the constant like normally.
> Example.whos_your_daddy
#=> :father
How is this behavior possible? By declaring the constants inside of an array, I would expect them to to be scoped inside the array. Please cite the relevant docs in your answer.
Edit: I suppose I'll clarify, the easiest way to answer this question is to explain two things:
First, what happens when the following code is executed:
PARENTS = [
FATHER = :father,
MOTHER = :mother
]
Second, does declare a constant anywhere tie it to the scope of the class it is being declared in? Why?