模块的实例变量是否在具有 mixin 的类之间共享?
我想知道 Ruby 模块的实例变量如何在“混合”它的多个类中表现。我写了一个示例代码来测试它:
# Here is a module I created with one instance variable and two instance methods.
module SharedVar
@color = 'red'
def change_color(new_color)
@color = new_color
end
def show_color
puts @color
end
end
class Example1
include SharedVar
def initialize(name)
@name = name
end
end
class Example2
include SharedVar
def initialize(name)
@name = name
end
end
ex1 = Example1.new("Bicylops")
ex2 = Example2.new("Cool")
# There is neither output or complains about the following method call.
ex1.show_color
ex1.change_color('black')
ex2.show_color
为什么它不起作用?有人可以解释一下 @color
在多个 Example$
实例中的实际行为吗?
I want to know how the instance variables of a Ruby module behaves across multiple classes which 'mix' it 'in'. I wrote a sample code to test it:
# Here is a module I created with one instance variable and two instance methods.
module SharedVar
@color = 'red'
def change_color(new_color)
@color = new_color
end
def show_color
puts @color
end
end
class Example1
include SharedVar
def initialize(name)
@name = name
end
end
class Example2
include SharedVar
def initialize(name)
@name = name
end
end
ex1 = Example1.new("Bicylops")
ex2 = Example2.new("Cool")
# There is neither output or complains about the following method call.
ex1.show_color
ex1.change_color('black')
ex2.show_color
Why it doesn't work? And Could someone explain what will the actual behavior of @color
across multiple Example$
instances?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Ruby 中,模块和类都是对象,因此可以为它们设置实例变量。
您的错误是认为变量 @color 与: 相同,
而
事实并非如此。
在第一个示例中,实例变量属于
SharedVar
对象,在第二个示例中,实例变量属于包含模块的对象。self 指针的另一种解释。在第一个示例中,self 指针设置为模块对象
SharedVar
,因此输入@color
将引用对象SharedVar< /code> 并且与另一个对象没有连接。在第二个示例中,方法
对象。因此在这种情况下,实例变量将引用show_color
只能在某个对象上调用,即ex1.show_color
,因此self指针将指向<代码>ex1ex1
对象。In Ruby modules and classes are objects, so it's possible to set instance variables for them.
Your mistake is thinking that the variable @color is the same for:
and
which is not.
In the first example, the instance variable belongs to the
SharedVar
object and in the second example the instance variable belongs to the object you include the module in.Another explanation by self pointer. In the first example the self pointer is set to the module object
SharedVar
, so typing@color
will refer to the objectSharedVar
and there's no connection with another object. In the second example, the methodshow_color
can be called only on some object, i.e.ex1.show_color
, so the self pointer will point toex1
object. So in this case the instance variable will refer toex1
object.您已经在类中包含了一个模块..所以实例变量@color应该是类Example1和Example2的实例变量
所以如果您想访问@color变量意味着您应该为该类创建一个对象并且然后你就可以访问
它
You have included a module in to a class.. so the instance variable @color should be an instance variable of both the class Example1 and Example2
So if you want to access the @color variable means you suppose to create an object to the class and then you can accesss it
For Ex