Assuming a module is included, not extended, what's difference between module instance variable and class variable?
假设包含一个模块,而不是扩展,模块实例变量和类变量之间有什么区别?
I do not see any difference between two.
我没有看到两者之间有任何区别。
module M
@foo = 1
def self.foo
@foo
end
end
p M.foo
module M
@@foo = 1
def self.foo
@@foo
end
end
p M.foo
I have been using @ as @@ within a module, and I recently saw other codes are using @@ within a module. Then I thought I might have been using it incorrectly.
我一直在模块中使用@ as @@,我最近看到其他代码在模块中使用@@。然后我想我可能一直在错误地使用它。
Since we cannot instantiate a module, there must be no difference between @ and @@ for a module. Am I wrong?
由于我们无法实例化模块,因此模块的@和@@之间必须没有区别。我错了吗?
----------------------- added the following --------------------
-----------------------添加以下内容--------------------
To answer some of questions on comments and posts, I also tested the following.
为了回答有关评论和帖子的一些问题,我还测试了以下内容。
module M
@foo = 1
def self.bar
:bar
end
def baz
:baz
end
end
class C
include M
end
p [:M_instance_variabies, M.instance_variables] # [@foo]
p [:M_bar, M.bar] # :bar
c = C.new
p c.instance_variables
p [:c_instance_variabies, c.instance_variables] # []
p [:c_baz, c.baz] :baz
p [:c_bar, c.bar] # undefined method
When you include a module within a class, module class variables and class methods are not defined in a class.
在类中包含模块时,模块类变量和类方法未在类中定义。
3 个解决方案
#1
5
Class variables can be shared between modules and classes where these modules are included.
类变量可以在包含这些模块的模块和类之间共享。
module A
@@a = 5
end
class B
include A
puts @@a # => 5
end
Meanwhile, instance variables belong to self
. When you include module A
into class B
, self
object of A
is not the same as self
object of B, therefore you will not be able to share instance variables between them.
同时,实例变量属于self。当您将模块A包含在B类中时,A的自身对象与B的自身对象不同,因此您将无法在它们之间共享实例变量。
module A
@a = 5
end
class B
include A
puts @a # => nil
end
#2
3
@@ refers to class variable and @ refers to instance variable. While using this with module makes big difference. When you include a module you add class methods to your class while extending add instance methods to your class
@@表示类变量,@表示实例变量。使用它与模块有很大的不同。当您包含模块时,可以向类中添加类方法,同时将实例方法扩展到类中
Following is a nice article on this
以下是一篇很好的文章
http://railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/
#3
0
I found this SO post that talks about how to create class variables in modules, "they are supported natively." One commenter even says the name 'class variable' is misleading since classes are just modules with a few extra powers.
我发现这篇SO帖子讨论了如何在模块中创建类变量,“它们本身就得到了支持。”一位评论者甚至说“类变量”这个名称具有误导性,因为类只是具有一些额外功能的模块。
All I'm sure of is nearly every article I've read about class variables thinks they're evil and to avoid them at all costs because of the weird inheritance issues you can encounter.
所有我确定的几乎每一篇关于类变量的文章都认为它们是邪恶的并且不惜一切代价避免它们,因为你可能会遇到奇怪的继承问题。
#1
5
Class variables can be shared between modules and classes where these modules are included.
类变量可以在包含这些模块的模块和类之间共享。
module A
@@a = 5
end
class B
include A
puts @@a # => 5
end
Meanwhile, instance variables belong to self
. When you include module A
into class B
, self
object of A
is not the same as self
object of B, therefore you will not be able to share instance variables between them.
同时,实例变量属于self。当您将模块A包含在B类中时,A的自身对象与B的自身对象不同,因此您将无法在它们之间共享实例变量。
module A
@a = 5
end
class B
include A
puts @a # => nil
end
#2
3
@@ refers to class variable and @ refers to instance variable. While using this with module makes big difference. When you include a module you add class methods to your class while extending add instance methods to your class
@@表示类变量,@表示实例变量。使用它与模块有很大的不同。当您包含模块时,可以向类中添加类方法,同时将实例方法扩展到类中
Following is a nice article on this
以下是一篇很好的文章
http://railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/
#3
0
I found this SO post that talks about how to create class variables in modules, "they are supported natively." One commenter even says the name 'class variable' is misleading since classes are just modules with a few extra powers.
我发现这篇SO帖子讨论了如何在模块中创建类变量,“它们本身就得到了支持。”一位评论者甚至说“类变量”这个名称具有误导性,因为类只是具有一些额外功能的模块。
All I'm sure of is nearly every article I've read about class variables thinks they're evil and to avoid them at all costs because of the weird inheritance issues you can encounter.
所有我确定的几乎每一篇关于类变量的文章都认为它们是邪恶的并且不惜一切代价避免它们,因为你可能会遇到奇怪的继承问题。