RUBY: how to resolve circular dependency in constant definitions?
class A
X = 9
Y = B::X
end
class B
X = 8
Y = A::X
end
I have two classes each defining some constants but requiring constants from the other as shown above but this gives开发者_运维百科 me an error:
circular.rb:7:in <class:A>': uninitialized constant A::B (NameError)
from circular.rb:5:in
'
Is there any way to resolve the error ?
Thanks.
It works if you split the definition of A into two parts:
class A
X = 9
end
class B
X = 8
Y = A::X
end
class A
Y = B::X
end
精彩评论