我如何在ruby中从父类继承包含继承的模块?

时间:2013-05-14 06:47:58

标签: ruby

如何让子类直接继承父包含的代码,而不必一直包含相同的模块?

例如:

class P
  include something
  ...
end

class C1
  include something
  ...
end

class C2
  include something
  ...
end

事实证明我犯了一个大错误,这不是我的错误的原因.... 真的很抱歉问一个愚蠢的问题.... :(

谢谢你们~~

2 个答案:

答案 0 :(得分:2)

module Includable
end

class Parent
  include Includable
end

class ChildOne < Parent
end

class ChildTwo < Parent
end

如果没有更清楚地理解这个问题,这将只是继承父母,而父母则包含模块,所以孩子们可以利用这个。

答案 1 :(得分:0)

子类自动继承父项包含的模块,你不需要做任何额外的事情。