gpt4 book ai didi

ruby - 如何添加到包含的模块

转载 作者:数据小太阳 更新时间:2023-10-29 07:51:40 24 4
gpt4 key购买 nike

考虑下面的代码,理想情况下,这将使我能够引用 Shelf::Things::ThingOneShelf::Things::ThingTwo:

module Things
class ThingOne
end
end

class Shelf
include Things
module Things
class ThingTwo
end
end
end

Shelf::Things.constants # => [:ThingTwo]

module Things 的第二个声明覆盖了第一个。是否可以重新打开包含的模块,以便可以在其中嵌套更多(例如)类?

编辑:事实证明这实际上不是我想要回答的问题。我问了一个跟进,here

最佳答案

The second declaration of module Things has overridden the first one

不,它没有。 Shelf::Things 是一个与 ::Things 完全无关的模块。

Things.constants # => [:ThingOne]
Shelf::Things.constants # => [:ThingTwo]

which would ideally result in me being able to refer to Shelf::Things::ThingOne and Shelf::Things::ThingTwo

module Things
class ThingOne
end
end

class Shelf
module Things
ThingOne = ::Things::ThingOne

class ThingTwo
end
end
end

Shelf::Things.constants # => [:ThingOne, :ThingTwo]

但是正如其他答案中已经暗示的那样,这种“继承”常量非常不合常理。您很可能为这项工作选择了错误的工具,但自we don't know the actual job以来很难判断。 .

关于ruby - 如何添加到包含的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49260832/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com