什么时候设置 Ruby 实例变量? [英] When do Ruby instance variables get set?

查看:20
本文介绍了什么时候设置 Ruby 实例变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Hello
@hello = "hello"
    def display
        puts @hello
    end
end

h = Hello.new
h.display

我创建了上面的类.它不会打印任何内容.我认为实例变量@hello 是在类声明期间设置的.但是当我调用 display 方法时,输出为nil".这样做的正确方法是什么?

I created the class above. It doesn't print anything out. I thought the instance variable @hello was set during the class declaration. But when I call the display method the output is 'nil'. What's the correct way to do this?

推荐答案

在第一次学习 Ruby 时,Ruby 中的实例变量可能会有些混乱,尤其是如果您已经习惯了另一种 OO 语言(如 Java).

Instance variables in ruby may be a bit confusing when first learning Ruby, especially if you are accustomed to another OO language like Java.

你不能简单地声明一个实例变量.

You cannot simply declare an instance variable.

关于 ruby​​ 中实例变量需要了解的最重要的事情之一,除了带有 @ 符号前缀的表示法之外,它们在第一次被赋值时就会出现..>

One of the most important things to know about instance variables in ruby, apart from the notation with an @ sign prefix, is that they spring into life the first time they are assigned to.

class Hello
  def create_some_state
    @hello = "hello"
  end
end

h = Hello.new
p h.instance_variables 

h.create_some_state
p h.instance_variables

# Output
[]
["@hello"]

您可以使用方法Object#instance_variables 列出对象的所有实例变量.

You can use the method Object#instance_variables to list all instance variables of an object.

您通常在 initialize 方法中声明"并初始化所有实例变量.明确记录哪些实例变量应该公开可用的另一种方法是使用模块方法 attr_accessor(读/写)、attr_writer(写)和 attr_reader(阅读).这些方法将为列出的实例变量合成不同的访问器方法.

You normally "declare" and initialize all the instance variables in the initialize method. Another way to clearly document which instance variables that should be publicly available is to use the Module methods attr_accessor (read/write), attr_writer (write) and attr_reader (read). These methods will synthesize different accessor methods for the listed instance variable.

class Hello
  attr_accessor :hello
end

h = Hello.new
p h.instance_variables 

h.hello = "hello"
p h.instance_variables

# Output
[]
["@hello"]

在使用合成的 Hello#hello= 方法将其分配给实例变量之前,它仍然不会被创建.

The instance variable still isn’t created until it’s assigned to using the synthesized Hello#hello= method.

另一个重要的问题,就像 kch 描述的那样,是你需要在声明一个类时注意不同的活动上下文.当声明一个类时,最外层作用域中的默认接收者(self)将是代表类本身的对象.因此,在类级别上分配给 @hello 时,您的代码将首先创建一个类实例变量.

Another important issue, like kch described, is that you need to be aware of the different contexts active when declaring a class. When declaring a class the default receiver (self) in the outermost scope will be the object that represents the class itself. Hence your code will first create a class instance variable when assigning to @hello on the class level.

Inside methods self 将是调用该方法的对象,因此您试图打印名称为 @hello 的实例变量的值对象,它不存在(请注意,读取不存在的实例变量是完全合法的).

Inside methods self will be the object on which the method is invoked, hence you are trying to print the value of an instance variable with the name @hello in the object, which doesn’t exists (note that it’s perfectly legal to read a non existing instance variable).

这篇关于什么时候设置 Ruby 实例变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆