且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将现有类添加到模块中

更新时间:2023-12-02 23:05:40

使用 const_missing 钩子.如果在当前模块中找不到该常量,请尝试在全局命名空间中解析:

Use the const_missing hook. If the constant can't be found in the current module, try to resolve in the global namespace:

class A; end
class B; end

module M
    def self.const_missing(c)
        Object.const_get(c)
    end
end

M::A.new
M::B.new