ruby 模块中的实例变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15478747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Instance variables in modules?
提问by Hommer Smith
How is it possible that I can have instance variables in a module even though I cannot create an instance of the module? What would be the purpose of @stackin module Stacklikebelow?
即使我无法创建模块的实例,我怎么可能在模块中拥有实例变量?下面@stack模块的目的是Stacklike什么?
module Stacklike
def stack
@stack ||= []
end
end
回答by mcfinnigan
Think of the instance variable as something which will exist in any class that includes your module, and things make a bit more sense:
将实例变量视为存在于包含您的模块的任何类中的东西,事情会更有意义:
module Stacklike
def stack
@stack ||= []
end
def add_to_stack(obj)
stack.push(obj)
end
def take_from_stack
stack.pop
end
end
class ClownStack
include Stacklike
def size
@stack.length
end
end
cs = ClownStack.new
cs.add_to_stack(1)
puts cs.size
will output "1"
将输出“1”
回答by Arup Rakshit
See the below:
请参阅以下内容:
p RUBY_VERSION
module Stacklike
def stack
@stack ||= []
end
def add_to_stack(obj)
stack.push(obj)
end
def take_from_stack
stack.pop
end
end
class A
include Stacklike
end
a = A.new
p a.instance_variables #<~~ E
p a.instance_variable_defined?(:@stack) #<~~ A
a.add_to_stack(10) #<~~ B
p a.instance_variable_defined?(:@stack) #<~~ C
p a.instance_variables #<~~ D
Output:
输出:
"1.9.3"
[]
false
true
[:@stack]
Explanation:Yes, Moduleinstance variables are present in the classwhen you would includethem inside the class. But you can see that p a.instance_variable_defined?(:@stack)is showing falseas @stackis still not defined till A. At point BI defined the instance variable @stack. Thus statement in point C, outputs as true. Means module instance variables are not being created by the module itself,but that can be done by the classinstances if the classincluded that module. Statement in Eoutputs []as still that point the instance variable was not defined, but if you see the output for the line D, it is proved the @stackis inside the object aof class A.
说明:是的,Module实例变量存在于class当你将include它们的类内。但是你可以看到它p a.instance_variable_defined?(:@stack)显示false为@stack尚未定义,直到A。在B点,我定义了实例变量 @stack。因此点C 中的语句输出为true。意味着模块实例变量不是由模块本身创建的,但是class如果class包含该模块,则可以由实例完成。E 中的语句[]仍然输出实例变量未定义的那一点,但是如果您看到D行的输出,则证明@stack位于 的对象a内class A。
Why such design?
为什么是这样的设计?
This is the design or sometimes come from the requirements. Say you have been asked to write a stack operation code which will be used by two ticket booking companies,Say Aand B. Now Aare stack policy for their customers to serve but also they have any more formalities with that. Bcompany also using stack policy with their own formalities which is different from A. Thus in case of designing Stackoperation inside class Aand class B, better idea to write it in a common place,as Both Aand Bhave this functionality common within them. In future if another company Ccomes to you you can also use that module into their class, without rewriting the same functionality for each A,Band C. There can be more thoughts but hope this will help you to answer your self for your last part of the questions.
这是设计或有时来自要求。假设您被要求编写一个堆栈操作代码,该代码将被两家订票公司 SayA和B. 现在A是他们的客户服务的堆栈政策,但他们还有更多的手续。B公司也使用堆栈策略与他们自己的手续不同A。因此,在设计的情况下,Stack操作中class A和class B,更好的办法是把它写在一个共同的地方,因为两者A并B在其中具有此功能常见。将来,如果另一家公司C来找您,您也可以在他们的类中使用该模块,而无需为每个模块重写相同的功能A,B并且C. 可以有更多的想法,但希望这能帮助你回答你自己的最后一部分问题。
That's all about the concept. Hope it helps.
这就是概念的全部。希望能帮助到你。
Cheers!!
干杯!!
回答by Chowlett
When you include a module in a class, all of its instance methods are effectively "pasted in" to the host class. So if you have:
当你在一个类中包含一个模块时,它的所有实例方法都有效地“粘贴”到宿主类中。所以如果你有:
class Lifo
include Stacklike
end
l = Lifo.new
l.add_to_stack(:widget)
Then lnow has an instance variable @stack, brought in from Stacklike.
然后l现在有一个实例变量@stack,从Stacklike.
回答by Erez Rabih
When you include the module Stacklike in some other class that instance variable will be available as if it was defined in that class. That give you the option to set and handle instance variables of the base class from the module itself.
当您在某个其他类中包含模块 Stacklike 时,该实例变量将可用,就像它是在该类中定义的一样。这使您可以选择从模块本身设置和处理基类的实例变量。

