Ruby中的绝对符号是什么意思?

时间:2021-05-02 16:58:23

For example

例如

func(@param) do |f|
    some code here
end

and

@param.each do |sth|
    some code here
end

What does the absolute value sign do here? I don't understand these two pieces of code.

这里的绝对值符号是什么?我不理解这两段代码。

4 个解决方案

#1


4  

It's the local variable within the block, so for the line:

它是块中的局部变量,因此对于行:

@param.each do |sth|

you're iterating over @param right, well each item in @param is referred to singularly as sth.

你在对@param进行迭代,@param中的每一项都被单独地称为sth。

So if @param refers to an array containing the numbers

所以如果@param指的是一个包含数字的数组

[1,3,5,4]

During the first iteration sth will be 1, then 3, then 5, then 4.

在第一次迭代中,sth将是1、3、5、4。

Same goes for :

同样适用于:

func(@param) do |f|

except now the local variable is called f! You could call it anything you want, even |ihavenoideawhatimdoing|

但是现在局部变量被称为f!你可以叫它任何你想要的名字,甚至| ihavenoidemdoing |

#2


2  

It's a local variable, it is saying that for the block of code between do...end, the variable f is defined.

它是一个局部变量,它表示对于do之间的代码块。最后,定义变量f。

#3


2  

It is a parameter to a block. The block is the part of the code between the do and the end. That block of code can use f or sth, which in your examples would probably have been set by func or each.

它是块的参数。块是do和end之间代码的一部分。这个代码块可以使用f或sth,在您的示例中,它们可能是由func或each设置的。

A tutorial on Ruby blocks will probably be helpful.

关于Ruby块的教程可能会有帮助。

Labmda calculus - more abstract, but it was the context in which I first saw these things.

Labmda的微积分——更抽象,但这是我第一次看到这些东西的背景。

#4


0  

It signifies instance variables. You often see it interchanged if people are using attr_* methods like attr_accessor, which makes @someattr and self.some_attr equivalent in instance methods.

它表示实例变量。如果人们使用attr_*方法(比如attr_accessor,它创建了@someattr和self),那么您经常会看到它的互换性。在实例方法中等效的some_attr。

#1


4  

It's the local variable within the block, so for the line:

它是块中的局部变量,因此对于行:

@param.each do |sth|

you're iterating over @param right, well each item in @param is referred to singularly as sth.

你在对@param进行迭代,@param中的每一项都被单独地称为sth。

So if @param refers to an array containing the numbers

所以如果@param指的是一个包含数字的数组

[1,3,5,4]

During the first iteration sth will be 1, then 3, then 5, then 4.

在第一次迭代中,sth将是1、3、5、4。

Same goes for :

同样适用于:

func(@param) do |f|

except now the local variable is called f! You could call it anything you want, even |ihavenoideawhatimdoing|

但是现在局部变量被称为f!你可以叫它任何你想要的名字,甚至| ihavenoidemdoing |

#2


2  

It's a local variable, it is saying that for the block of code between do...end, the variable f is defined.

它是一个局部变量,它表示对于do之间的代码块。最后,定义变量f。

#3


2  

It is a parameter to a block. The block is the part of the code between the do and the end. That block of code can use f or sth, which in your examples would probably have been set by func or each.

它是块的参数。块是do和end之间代码的一部分。这个代码块可以使用f或sth,在您的示例中,它们可能是由func或each设置的。

A tutorial on Ruby blocks will probably be helpful.

关于Ruby块的教程可能会有帮助。

Labmda calculus - more abstract, but it was the context in which I first saw these things.

Labmda的微积分——更抽象,但这是我第一次看到这些东西的背景。

#4


0  

It signifies instance variables. You often see it interchanged if people are using attr_* methods like attr_accessor, which makes @someattr and self.some_attr equivalent in instance methods.

它表示实例变量。如果人们使用attr_*方法(比如attr_accessor,它创建了@someattr和self),那么您经常会看到它的互换性。在实例方法中等效的some_attr。