Ruby 中的发送方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12892045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:27:40  来源:igfitidea点击:

send method in Ruby

ruby

提问by Xitcod13

I just read about what senddoes in Ruby and I'm still confused when looking at this code (it's from a quiz but its past due date anyways)

我刚刚阅读了sendRuby 中的内容,但在查看此代码时仍然感到困惑(它来自测验,但无论如何它已过期)

x = [1,2,3]
x.send :[]=,0,2
x[0] + x.[](1) + x.send(:[],2)

I understand that the first line assigns an array to x then I don't understand what :[] = ,0,2does at all and i dont understand why send is needed there I dont get what x.[](1)does and x.send(:[],2) do on the last line

我知道第一行将一个数组分配给 x 然后我根本不明白有什么:[] = ,0,2作用,我不明白为什么需要在那里发送我不明白x.[](1)x.send(:[],2) 在最后做了什么线

I'm really confused and I just cant find this information online.

我真的很困惑,我只是无法在网上找到这些信息。

I found the what send doesbut I'm still a little bit confused and a lot of bit confused about this code as a whole.

我发现send 做了什么但我仍然有点困惑,并且对整个代码感到有些困惑。

回答by Chris Heald

First of all, things like [](array index) and []=are just methods in Ruby. xis an Array, and arrays have a []=method, which accepts two arguments, an index and a value to set.

首先,像[](array index) 和[]=只是 Ruby 中的方法。x是一个Array,数组有一个[]=方法,它接受两个参数,一个索引和一个要设置的值。

Using sendlets you pass an arbitrary "message" (method call) to object, with arbitrary parameters.

Usingsend允许您使用任意参数将任意“消息”(方法调用)传递给对象。

You could call x.send :sort, for example, to send the "sort" message to the array. Sort doesn't need any parameters, though, so we don't have to pass anything extra to it.

x.send :sort例如,您可以调用将“排序”消息发送到数组。不过,Sort 不需要任何参数,所以我们不必向它传递任何额外的东西。

x#[]=, on the other hand, accepts two arguments. Its method can be thought of to look like this:

x#[]=,另一方面,接受两个参数。它的方法可以被认为是这样的:

def []=(index, value)
  self.set_value_at_index(index, value)
end

So, we can just invoke it with send :[]=, 0, 2, which is just like calling x[0] = 2. Neat, huh?

所以,我们可以用 调用它send :[]=, 0, 2,就像调用x[0] = 2. 整洁吧?

回答by Eureka

In Ruby, a[0] = 2is actually syntactic sugar for a.[]=(0, 2).

在 Ruby 中,a[0] = 2实际上是a.[]=(0, 2).

Knowing this, that's what your second line does—it calls the []=method with two arguments using metaprogramming, as you correctly guessed.

知道这一点,这就是你的第二行所做的——它[]=使用元编程调用带有两个参数的方法,正如你猜对的那样。

This is the same for your third line: a[0]is syntactic sugar in Ruby for x.[](0).

这与您的第三行相同:a[0]是 Ruby 中的语法糖 for x.[](0).

The following code is a simpler equivalent to your example:

以下代码与您的示例更简单:

x = [1, 2, 3]
x[0] = 2
x[0] + x[1] + x[2]

回答by Shoe

Don't worry. Ruby can be a little bit tricky in these cases. Let's examine the code line by line:

别担心。在这些情况下,Ruby 可能有点棘手。让我们一行一行地检查代码:

x = [1,2,3]
x.send :[]=,0,2
x[0] + x.[](1) + x.send(:[],2)

First line

第一行

First line is clear to you: it assigns an array of three elements to x. And that's just about it.

第一行对您来说很清楚:它将三个元素的数组分配给 x。仅此而已。

Second line

第二行

Second line calls the Object#sendmethod of xpassing a symbol (remember that everything that starts with :is a symbol in ruby) :[]=, 0(a Fixnum) and 2(another Fixnum).
Now you just have to take a look at what the send method does (as you said you've already done):

第二行调用传递符号的Object#send方法x(记住,所有:以 ruby开头的都是符号):[]=0(a Fixnum) 和2(another Fixnum)。
现在你只需要看看 send 方法做了什么(正如你所说的那样):

Invokes the method identified by symbol, passing it any arguments specified

调用由符号标识的方法,将指定的任何参数传递给它

This means that it will invoke the method identified by :[]=and pass 0and 2to it. Now let's take a look at the Array#[]=method. This method definition (which can be overloadedby you if you need to do so) is:

这意味着它将调用由所标识的方法:[]=和通过02给它。现在让我们来看看Array#[]=方法。此方法定义(如果需要,可以由您重载)是:

class Array
    # ...
    def []=(a, b)
        # ...
    end
end

This method is called by sendas x.[]=(0, 2)which is pretty ugly if you ask me. That's why Ruby defines a syntactic sugar version: x[0] = 2and in general:

如果你问我,这个方法是由sendas调用的,x.[]=(0, 2)这很丑陋。这就是为什么 Ruby 定义了一个语法糖版本:x[0] = 2一般来说:

x.[]=(a, b)  -->  x[a] = b

In the Arraycase we also have the following:

在这种Array情况下,我们还有以下内容:

x.[](a)  -->  x[a]

In both cases you are free to call whatever version makes sense to you in the specific context.

在这两种情况下,您都可以自由调用在特定上下文中对您有意义的任何版本。

Third line

第三行

Now for the third and last line:

现在是第三行也是最后一行:

x[0] + x.[](1) + x.send(:[],2)

things gets really tricky. Let's divide it into:

事情变得非常棘手。我们把它分成:

  1. x[0]
  2. x.[](1)
  3. x.send(:[], 2)
  1. x[0]
  2. x.[](1)
  3. x.send(:[], 2)

The first one is pretty straight forward. It returns the first element of the array.

第一个非常简单。它返回数组的第一个元素。

The second one is the syntactic sugar that we have seen earlier which can be basically be convertedinto x[1]which returns the second element of the array.

第二个是我们之前看到的语法糖,基本上可以转换x[1]它返回数组的第二个元素。

The third one uses Object#sendto call the method []passing 2to it. Which means that it calls x.[](2)which means x[2].

第三个用于Object#send调用[]传递2给它的方法。这意味着它调用x.[](2)which 意味着x[2]

Conclusion

结论

The code

编码

x = [1,2,3]
x.send :[]=,0,2
x[0] + x.[](1) + x.send(:[],2)

can be converted using:

可以使用以下方法转换:

x.send(:[]=, a, b)  -->  x.[]=(a, b)
x.send(:[], a)  -->  x.[](a)
x.[]=(a, b)  -->  x[a] = b
x.[](a)  -->  x[a]

to:

到:

x = [1,2,3]
x[0] = 2
x[0] + x[1] + x[2]

which can be reduced to:

可以简化为:

2 + 2 + 3

which results in:

这导致:

7

回答by Boris Strandjev

sendis a way to achieve reflection calls in ruby. Thus this line:

send是一种在 ruby​​ 中实现反射调用的方法。因此这一行:

x.send :[]=,0,2

Is equivalent to:

相当于:

x[0] = 2

you read it that way: the name of the method is symbol (in your case []=) and then you pass in the parameters - 0 and 2.

你是这样读的:方法的名称是符号(在你的例子中[]=),然后你传入参数 - 0 和 2。

回答by user2592489

x[0]+x[1]+x[2]=2+2+3=7 i guess this must be the answer

x[0]+x[1]+x[2]=2+2+3=7 我想这一定是答案