Ruby on rails rails库目录中的模块
考虑另一个模块Ruby on rails rails库目录中的模块,ruby-on-rails,module,instantiation,instance-variables,Ruby On Rails,Module,Instantiation,Instance Variables,考虑另一个模块OtherModule,其中包括MyModule。当OtherModule中的代码多次调用my\u方法时,@instance\u variable是否会被多次实例化?还是就一次?更新答案: 如果委托模块调用该方法,您将得到一个错误 如果原始模块调用该方法,则类实例变量仅实例化1次 如果你想在一个模块中定义“类方法”,你不能定义self.xx,然后简单地扩展/包含它。您应该“扩展”它,或者“将其包含到本征类”,例如 modulesomemodule def self.aim_to_b
OtherModule
,其中包括MyModule
。当OtherModule
中的代码多次调用my\u方法时,@instance\u variable
是否会被多次实例化?还是就一次?更新答案:
如果委托模块调用该方法,您将得到一个错误
如果原始模块调用该方法,则类实例变量仅实例化1次
如果你想在一个模块中定义“类方法”,你不能定义self.xx,然后简单地扩展/包含它。您应该“扩展”它,或者“将其包含到本征类”,例如
modulesomemodule
def self.aim_to_be_class_方法;结束;
def aim_to_be_instance_方法;结束;
结束
#类方法:[]
#实例方法:[“目标为实例方法”]
类包括模块的一些类
包括一些模块
结束
#类方法:[“aim\u to\u be\u instance\u method”]
#实例方法:[]
类SomeClass扩展模块
扩展SomeModule
结束
#类方法:[“aim\u to\u be\u instance\u method”]
#实例方法:[]
类SomeClassMadeeIgEncludingModule
第127页的类,您可以将“类实例变量”看作java的静态字段。所以,我认为大多数情况下,它应该运行一次
有关更多详细信息,请编写一个单元测试,看看会发生什么?稍后,我将用自己编写的单元测试代码更新我的答案
更新:我的单元测试和结果:
module SomeModule
def self.aim_to_be_class_method ; end;
def aim_to_be_instance_method ; end;
end
# class methods: []
# instance methods: ["aim_to_be_instance_method"]
class SomeClassIncludingTheModule
include SomeModule
end
# class methods: ["aim_to_be_instance_method"]
# instance methods: []
class SomeClassExtendingTheModule
extend SomeModule
end
# class methods: ["aim_to_be_instance_method"]
# instance methods: []
class SomeClassMadeEigenClassIncludingModule
class << self
include SomeModule
end
end
#所有代码都放在一个文件中:class_instance_variable_test.rb
#定义一个类以便我们看到它的初始化过程
苹果类
def初始化
放入“苹果初始化~”
结束
结束
#定义一个名为Apple.new的原始_模块
模块原始模块
def self.u方法
@var | |=苹果新
结束
结束
#定义另一个模块以调用原始的_模块
模块委派模块
包含原始模块
结束
#现在测试开始了
需要“测试/单元”
类ClassInstanceTest
module SomeModule
def self.aim_to_be_class_method ; end;
def aim_to_be_instance_method ; end;
end
# class methods: []
# instance methods: ["aim_to_be_instance_method"]
class SomeClassIncludingTheModule
include SomeModule
end
# class methods: ["aim_to_be_instance_method"]
# instance methods: []
class SomeClassExtendingTheModule
extend SomeModule
end
# class methods: ["aim_to_be_instance_method"]
# instance methods: []
class SomeClassMadeEigenClassIncludingModule
class << self
include SomeModule
end
end
# all of the code is placed in a file: class_instance_variable_test.rb
# define a class so that we see its initialize process
class Apple
def initialize
puts "Apple initialized~"
end
end
# define an original_module that calls Apple.new
module OriginalModule
def self.original_method
@var ||= Apple.new
end
end
# define another module to call the original_module
module DelegateModule
include OriginalModule
end
# now the test begins
require 'test/unit'
class ClassInstanceTest < Test::Unit::TestCase
# output of this test case:
# NoMethodError: undefined method `original_method' for DelegateModule:Module
def test_if_delegate_module_could_call_original_method_by_including_original_module
DelegateModule.original_method
end
# output of this test case:
# ----- calling from original_method, 3 times called, how many times initialized?
# Apple initialized~
# ------ ends
def test_if_we_could_call_original_method_from_original_module
puts "----- calling from original_method, 3 times called, how many times initialized? "
OriginalModule.original_method
OriginalModule.original_method
OriginalModule.original_method
puts "------ ends "
end
end