【发布时间】:2011-09-03 21:11:45
【问题描述】:
您可以调用 include 将模块与 ruby 中的类混合,但必须在类定义的开头完成。为什么不能在类函数中完成?有其他语法吗?
前:
module UsefulThings
def a() puts "a" end
end
class IncludeTester
include UsefulThings
def initialize
end
end
n = IncludeTester.new
n.a()
^^ 这可行,但如果我将 IncludeTester 更改为以下内容,我会收到错误“未定义的方法 `include'”
class IncludeTester
def initialize
include UsefulThings
end
end
【问题讨论】:
标签: ruby