获取模块中类的可见性的最佳方法

Gar*_*ary 4 ruby

有关最佳方法的建议,不需要使用'::'来使用模块中的类(如下所示)?

使用import/mixin?这不是一个多继承案例吗?只想简化代码.在这种情况下使用导入是否有副作用?

module Shapes  
    class Circle  
    class RightTriangle  
end

class ShapeUser  
    def go  
        shape1 = Shapes::Circle.new   
        ** prefer to use just shape1 = Circle.new
        ....
end
Run Code Online (Sandbox Code Playgroud)

hor*_*guy 5

include 模块:

class ShapeUser
  include Shapes

  def go
    shape1 = Circle.new
    # ...
  end
end
Run Code Online (Sandbox Code Playgroud)

所有这一切都发生在这里的是你正在常数Circle,RightTriangle等提供给类ShapeUser.这是一个完全合理的使用include:)

  • @Gary - 别忘了点击栏杆答案旁边的复选标记,将其标记为已接受. (2认同)