Add existing classes into a module

前端 未结 5 2111
无人及你
无人及你 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 02:49

    Yes, in your module create the class and have it inherit from your outside classes. For example,

    class A
    ...
    end
    
    module MyModule
     class NewA < A
     end
    end
    

    The class MyModule::NewA will have all the attributes and methods of class A.
    Then again, modules in ruby are never locked, so there is nothing stopping you just writing the class definition straight into the module.

提交回复
热议问题