Add existing classes into a module

前端 未结 5 2105
无人及你
无人及你 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条回答
  •  萌比男神i
    2020-12-17 02:47

    module Foo
      A = ::A
      B = ::B
    end
    
    Foo::A.new.bar
    

    Note that the :: prefix on a constant starts searchign the global namespace first. Like a leading / on a pathname. This allows you differentiate the global class A from the modularized constant Foo::A.

提交回复
热议问题