且构网

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

将现有的类添加到模块中

更新时间:2023-02-16 23:32:31

使用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