Add existing classes into a module

前端 未结 5 2101
无人及你
无人及你 2020-12-17 02:37

I have some existing ruby classes in a app/classes folder:

class A
   ...
end

class B
   ...
end

I\'d like to group those classes in a mod

5条回答
  •  一整个雨季
    2020-12-17 03:06

    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
    

提交回复
热议问题