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

查看:83
本文介绍了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.

关于红宝石中的实例变量,除了带@符号前缀的符号外,最重要的事情之一就是它们在首次分配给它们时就崭露头角. >

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所述,是在声明类时需要注意活跃的不同上下文.当声明一个类时,最外层的默认接收者(自己)将是代表该类本身的对象.因此,在类级别分配给@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.

内部方法 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天全站免登陆