我有两个文件,一个带有模块ToMix
:
module ToMix
@module_var = "modulevar"
def self.mix_function
puts "mix_function SELF: #{@module_var}"
end
def mix_function
puts "mix_function: #{@module_var}"
end
class MixClass
attr_accessor :mixclassvar
def initialize(value)
@mixclassvar = value
end
end
end
我想将其混入TestInclude
另一个文件中的类:
class TestInclude
require "ToMixFile"
include ToMix
end
有人可以解释为什么实例变量@module_var
和方法未定义吗self.mix_function
?mix_function
我将如何定义它们?
t2 = TestInclude.new()
t2.mix_function # => error undefined (exected call to mix_function)
t2.module_var = "test set module_var" # => error undefined
TestInclude.mix_function # => error undefined (expected call to self.mix_function)
TestInclude.method_defined? :mix_function # => false