模块的实例变量是否在具有 mixin 的类之间共享?

发布于 2025-01-08 19:07:20 字数 749 浏览 0 评论 0原文

我想知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不交电费瞎发啥光 2025-01-15 19:07:20

在 Ruby 中,模块和类都是对象,因此可以为它们设置实例变量。

module Test
  @test = 'red'
  def self.print_test
    puts @test
  end
end

Test.print_test #=> red

您的错误是认为变量 @color 与: 相同,

module SharedVar
  @color
end

module SharedVar
  def show_color
    @color
  end
end

事实并非如此。

在第一个示例中,实例变量属于 SharedVar 对象,在第二个示例中,实例变量属于包含模块的对象。

self 指针的另一种解释。在第一个示例中,self 指针设置为模块对象 SharedVar,因此输入 @color 将引用对象 SharedVar< /code> 并且与另一个对象没有连接。在第二个示例中,方法show_color只能在某个对象上调用,即ex1.show_color,因此self指针将指向<代码>ex1 对象。因此在这种情况下,实例变量将引用 ex1 对象。

In Ruby modules and classes are objects, so it's possible to set instance variables for them.

module Test
  @test = 'red'
  def self.print_test
    puts @test
  end
end

Test.print_test #=> red

Your mistake is thinking that the variable @color is the same for:

module SharedVar
  @color
end

and

module SharedVar
  def show_color
    @color
  end
end

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 object SharedVar and there's no connection with another object. In the second example, the method show_color can be called only on some object, i.e. ex1.show_color, so the self pointer will point to ex1 object. So in this case the instance variable will refer to ex1 object.

清风挽心 2025-01-15 19:07:20

您已经在类中包含了一个模块..所以实例变量@color应该是类Example1和Example2的实例变量

所以如果您想访问@color变量意味着您应该为该类创建一个对象并且然后你就可以访问

irb(main):028:0> ex1.change_color('black')
=> "black"
irb(main):029:0> ex1.show_color
black
irb(main):031:0> ex2.change_color('red')
=> "red"
irb(main):032:0> ex2.show_color
red

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

irb(main):028:0> ex1.change_color('black')
=> "black"
irb(main):029:0> ex1.show_color
black
irb(main):031:0> ex2.change_color('red')
=> "red"
irb(main):032:0> ex2.show_color
red
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文