RUBY:如何解决常量定义中的循环依赖关系? [英] RUBY: how to resolve circular dependency in constant definitions?
本文介绍了RUBY:如何解决常量定义中的循环依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
A类
X = 9
Y = B :: X
end
B类
X = 8
Y = A :: X
end
我有两个类,每个类都定义了一些常量,但又需要一个常量
,如上所示,但这给了我一个错误: $ c>< class:A>':未初始化的常量A :: B(NameError)
from Circular.rb:5:in '
有什么办法可以解决该错误?
谢谢。
解决方案
如果将A的定义分为两部分,它将起作用:
class A
X = 9
结束
B类
X = 8
Y = A :: X
结束
A类
Y = B :: X
结束
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
这篇关于RUBY:如何解决常量定义中的循环依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文