赋值符号是如何工作的 - Ruby

时间:2021-01-21 22:29:32

In Ruby if i just assign a local variable.

在Ruby中如果我只是分配一个局部变量。

sound = "bang". 

is that a main.sound=("bang") method? if so, where and how is that method "sound=" being defined? or how is that assignment working? if not, what is actually happening?

是一个main.sound =(“bang”)方法?如果是这样,那个方法“sound =”在哪里以及如何定义?或者这项任务如何运作?如果没有,实际发生了什么?

i know that for a setter method you would say x.sound=("bang"). and you are calling the method "sound=" on the object "x" with the argument "bang". and you are creating an instance variable "sound".

我知道对于一个setter方法,你会说x.sound =(“bang”)。并且您使用参数“bang”在对象“x”上调用方法“sound =”。你正在创建一个实例变量“声音”。

and i can picture all of that. but not when you assign a variable in the "main" object. as far as i know it isn't an instance variable of the Object class... or is it? I'm so confused.

我可以想象所有这一切。但不是在“主”对象中分配变量时。据我所知,它不是Object类的实例变量......或者是它?我很困惑。

3 个解决方案

#1


6  

In most programming languages, Ruby included, assignment is a strange beast. It is not a method or function, what it does is associate a name (also called an lvalue since it's left of the assignment) with a value.

在大多数编程语言中,包含Ruby,赋值是一种奇怪的野兽。它不是一个方法或函数,它所做的是将一个名称(也称为左值,因为它左侧的赋值)与一个值相关联。

Ruby adds the ability to define methods with names ending in = that can be invoked using the assignment syntax.

Ruby增加了定义名称以=结尾的方法的能力,可以使用赋值语法调用这些方法。

Attribute accessors are just methods that create other methods that fetch and assign member variables of the class.

属性访问器只是创建其他方法的方法,这些方法可以获取和分配类的成员变量。

So basically there are 3 ways you see assignment:

所以基本上有3种方法可以看出作业:

  • the primitive = operator
  • primitive =运算符

  • methods with names ending in =
  • 名称以=结尾的方法

  • methods generated for you by the attribute accessor (these are methods ending in =)
  • 属性访问器为您生成的方法(这些方法以=结尾)

#2


1  

A variable assignment is just creating a reference to an object, like naming a dog "Spot". The "=" is not calling any method whatsoever.

变量赋值只是创建对象的引用,比如命名狗“Spot”。 “=”不会调用任何方法。

As @ZachSmith comments, a simple expression such as sound could refer to a local variable named "sound"or a method of selfnamed "sound". To resolve this ambiguity, Ruby treats an identifier as a local variable if it has "seen" a previous assignment to the variable.

正如@ZachSmith所评论的,一个简单的表达式如声音可以引用一个名为“声音”的局部变量或一个自称为“声音”的方法。为了解决这种歧义,Ruby将标识符视为局部变量,如果它已“看到”先前对​​变量的赋值。

#3


0  

is that a main.sound=("bang") method?

是一个main.sound =(“bang”)方法?

No. main.sound="bang" should set instance variable or element of that variable.
With dot(main.sound) you tell object to do some method(in this case sound).

编号main.sound =“bang”应该设置实例变量或该变量的元素。使用dot(main.sound),你告诉对象做一些方法(在这种情况下是声音)。

To manage local variables ruby create new scope.

为了管理局部变量,ruby创建了新的范围。

class E
  a = 42

  def give_a
    puts a
  end

  def self.give_a
    puts a
  end
  binding 
end
bin_e = _ # on pry
E.give_a     # error
E.new.give_a # error

Both methods doesn't know about a. After you create your class, a will soon disappear, deleted by garbage collector. However you can get that value using binding method. It save local scope to some place and you can assign it to the variable.

这两种方法都不知道a。创建类后,很快就会消失,被垃圾收集器删除。但是,您可以使用绑定方法获取该值。它将本地范围保存到某个地方,您可以将其分配给变量。

bin.eval "a" # 42

lambdas have scope where they were defined:

lambdas具有定义它们的范围:

local_var_a = 42
lamb = ->{puts local_var_a} 
lamb.call() # 42

#1


6  

In most programming languages, Ruby included, assignment is a strange beast. It is not a method or function, what it does is associate a name (also called an lvalue since it's left of the assignment) with a value.

在大多数编程语言中,包含Ruby,赋值是一种奇怪的野兽。它不是一个方法或函数,它所做的是将一个名称(也称为左值,因为它左侧的赋值)与一个值相关联。

Ruby adds the ability to define methods with names ending in = that can be invoked using the assignment syntax.

Ruby增加了定义名称以=结尾的方法的能力,可以使用赋值语法调用这些方法。

Attribute accessors are just methods that create other methods that fetch and assign member variables of the class.

属性访问器只是创建其他方法的方法,这些方法可以获取和分配类的成员变量。

So basically there are 3 ways you see assignment:

所以基本上有3种方法可以看出作业:

  • the primitive = operator
  • primitive =运算符

  • methods with names ending in =
  • 名称以=结尾的方法

  • methods generated for you by the attribute accessor (these are methods ending in =)
  • 属性访问器为您生成的方法(这些方法以=结尾)

#2


1  

A variable assignment is just creating a reference to an object, like naming a dog "Spot". The "=" is not calling any method whatsoever.

变量赋值只是创建对象的引用,比如命名狗“Spot”。 “=”不会调用任何方法。

As @ZachSmith comments, a simple expression such as sound could refer to a local variable named "sound"or a method of selfnamed "sound". To resolve this ambiguity, Ruby treats an identifier as a local variable if it has "seen" a previous assignment to the variable.

正如@ZachSmith所评论的,一个简单的表达式如声音可以引用一个名为“声音”的局部变量或一个自称为“声音”的方法。为了解决这种歧义,Ruby将标识符视为局部变量,如果它已“看到”先前对​​变量的赋值。

#3


0  

is that a main.sound=("bang") method?

是一个main.sound =(“bang”)方法?

No. main.sound="bang" should set instance variable or element of that variable.
With dot(main.sound) you tell object to do some method(in this case sound).

编号main.sound =“bang”应该设置实例变量或该变量的元素。使用dot(main.sound),你告诉对象做一些方法(在这种情况下是声音)。

To manage local variables ruby create new scope.

为了管理局部变量,ruby创建了新的范围。

class E
  a = 42

  def give_a
    puts a
  end

  def self.give_a
    puts a
  end
  binding 
end
bin_e = _ # on pry
E.give_a     # error
E.new.give_a # error

Both methods doesn't know about a. After you create your class, a will soon disappear, deleted by garbage collector. However you can get that value using binding method. It save local scope to some place and you can assign it to the variable.

这两种方法都不知道a。创建类后,很快就会消失,被垃圾收集器删除。但是,您可以使用绑定方法获取该值。它将本地范围保存到某个地方,您可以将其分配给变量。

bin.eval "a" # 42

lambdas have scope where they were defined:

lambdas具有定义它们的范围:

local_var_a = 42
lamb = ->{puts local_var_a} 
lamb.call() # 42