ruby - How to get a reference to a 'dynamic' object call? -


Good, my question is not very clear, but what I want to do is:

  Average = [1, 2, 3]. Inject (0) {| Sum, el | Sum + el} / this.size   

The above code will not work on this due to the ridiculous call, but what I want to achieve is a reference to the array In which I am injected (in this case [1, 2, 3]), I did it with this while looking at my ignorance in Ruby, but can you please tell me How to do that? Is it possible to do without a variable reference at all?

Thanks in advance!

In Ruby there is not that the closest thing is itself.

Here are some examples to help you

  #example 1 is not a required number of self array numbers = [1, 2, 3] numbers. (+) To_f / numbers.size # Example 2 which gives access to self and gives self # So why the total variable is needed = = [1, 2, 3]. Tap {| A. Total = a.reduce (: +). To_f / a.size} #Instant_Aval hack that reaches itself, and the latter block is an expression # can return the average without an additional variable [1, 2, 3] .instance_eval {Self.reduce (: +) . To_f / self.size} # = & gt; 2.0   

So far I prefer Example 1

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

ruby on rails - Convert a Activerecord result with has_one associations into a single hash per record -

javascript - Read complete data from stream from onMessage -