如果我有一堆类似于:
module A
module B
module C
module D
class SpecificClass1
end
end
end
end
end
而且我不想像
那样引用这个课程 instance = A::B::C::D::SpecificClass1.new
一种方法是:
include A::B::C::D
instance = SpecificClass1.new
如果我想做这样的事情怎么办?
include A::B
instance = C::D::SpecificClass1.new
为什么这不起作用?它返回:
uninitialized constant C::D (NameError)
有没有办法有效地做我尝试的事情?
答案 0 :(得分:0)
来自irb会议:
module A
module B
module C
module D
class SpecificClass1
end
end
end
end
end
=> nil
2.1.0 :011 > A::B::C::D::SpecificClass1.new
=> #<A::B::C::D::SpecificClass1:0x0000010248f3c0>
2.1.0 :012 > include A::B::C::D
=> Object
2.1.0 :013 > instance = SpecificClass1.new
=> #<A::B::C::D::SpecificClass1:0x00000103803b40>
2.1.0 :011 > include A::B
=> Object
2.1.0 :012 > instance = C::D::SpecificClass1.new
=> #<A::B::C::D::SpecificClass1:0x0000010247b848>