如何获得对“动态”对象调用的引用?

时间:2022-12-03 16:54:55

Well, my question is not very clear, but what I want to do is the following:

好吧,我的问题不是很清楚,但我想做的是以下几点:

average = [1, 2, 3].inject(0) { |sum, el| sum + el } / this.size

The code above won't work because of the ridiculous call to this, But what I want to accomplish is to get a reference to the array to which I'm calling inject on. (in this case [1, 2, 3]), Given my ignorance in ruby I did it with this. But, Could you please tell me how to do it right? Is it possible at all to do it without a variable reference?

上面的代码因为对此的荒谬调用而无法工作,但我想要实现的是获取对我所调用的数组的引用。 (在这种情况下[1,2,3]),鉴于我对红宝石的无知,我用它做了。但是,你能告诉我怎么做对吗?没有变量引用就可以做到这一点吗?

Thanks in advance!

提前致谢!

2 个解决方案

#1


4  

There is no this in ruby the nearest thing is self.

红宝石中没有这个,最接近的是自我。

Here are some examples to help you on your way

以下是一些可以帮助您的示例

#example 1 not self needed numbers is the array

numbers = [1, 2, 3]

numbers.reduce(:+).to_f / numbers.size

# example 2 using tap which gives access to self and returns self
# hence why total variable is needed

total = 0
[1, 2, 3].tap {|a| total = a.reduce(:+).to_f / a.size }

# instance_eval hack which accesses self, and the block after do is an expression 
# can return the average without an extra variable

[1, 2, 3].instance_eval { self.reduce(:+).to_f / self.size } # => 2.0

So far I prefer example 1

到目前为止,我更喜欢示例1

#2


0  

'this' doesn't refer to the array, so that won't work at all. I don't think it's possible to get a reference to the array as you have declared it. But you will never run into such a problem, because if the array is hardwired into the code like that, then the divisor might as well be too, so you can just write '/3' at the end.

'this'不是指数组,因此根本不起作用。我不认为可以像你声明的那样获得对数组的引用。但是你永远不会遇到这样的问题,因为如果数组被硬编码到这样的代码中,那么除数也可能也是如此,所以你最后可以写'/ 3'。

In the general case where you have an array of unknown size, you would also have a name for the array, so you could use that. Like:

在一般情况下,如果你有一个未知大小的数组,你也可以有一个数组的名称,所以你可以使用它。喜欢:

a = [1, 2, 3]
average = a.inject(0) {|sum, el| sum+el} / a.size

#1


4  

There is no this in ruby the nearest thing is self.

红宝石中没有这个,最接近的是自我。

Here are some examples to help you on your way

以下是一些可以帮助您的示例

#example 1 not self needed numbers is the array

numbers = [1, 2, 3]

numbers.reduce(:+).to_f / numbers.size

# example 2 using tap which gives access to self and returns self
# hence why total variable is needed

total = 0
[1, 2, 3].tap {|a| total = a.reduce(:+).to_f / a.size }

# instance_eval hack which accesses self, and the block after do is an expression 
# can return the average without an extra variable

[1, 2, 3].instance_eval { self.reduce(:+).to_f / self.size } # => 2.0

So far I prefer example 1

到目前为止,我更喜欢示例1

#2


0  

'this' doesn't refer to the array, so that won't work at all. I don't think it's possible to get a reference to the array as you have declared it. But you will never run into such a problem, because if the array is hardwired into the code like that, then the divisor might as well be too, so you can just write '/3' at the end.

'this'不是指数组,因此根本不起作用。我不认为可以像你声明的那样获得对数组的引用。但是你永远不会遇到这样的问题,因为如果数组被硬编码到这样的代码中,那么除数也可能也是如此,所以你最后可以写'/ 3'。

In the general case where you have an array of unknown size, you would also have a name for the array, so you could use that. Like:

在一般情况下,如果你有一个未知大小的数组,你也可以有一个数组的名称,所以你可以使用它。喜欢:

a = [1, 2, 3]
average = a.inject(0) {|sum, el| sum+el} / a.size