gpt4 book ai didi

Ruby - 在模块而不是顶层获取 const_missing

转载 作者:太空宇宙 更新时间:2023-11-03 17:59:57 24 4
gpt4 key购买 nike

我有一个名为 Plants 的模块,其中有一个 const_missing 方法和一个名为 Sunflower 的类。当我调用未定义的常量时,不会调用模块内的 const_missing 方法。而是调用 Object 类中的 const_missing。有没有办法让模块内部的 const_missing 被触发?

module Plants
def self.const_missing(name)
puts name
end

class Sunflower
def some_method
Grass
end
end
end


obj = Plants::Sunflower.new
obj.some_method

提前致谢! :)

最佳答案

module Plants

def const_missing(name)
puts name
end

class Sunflower
extend Plants
def some_method
Grass
end
end
end

关于Ruby - 在模块而不是顶层获取 const_missing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6840161/

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