Procs From "Understanding Ruby Blocks, Procs, and Lambdas" Article -


OK, through this review of process, lumber, and block.

Question on this code:

Repeat square array DET! Self.each_with_index do | N, i | Itself [i] = yield (n) end end end array = [1, 2, 3, 4] array.iterate! Do N | N ** 2 puts the end array. Inspect

Conceptually, I consider almost everything except one thing:

itself [i] = yield (n)

I think that in this line it will be written by itself self.each_with_index do. N, i. means that this is a class method, is not it?

But why do we need to allocate parameters in the yield (n) to my [i] ?

Please explain in a super basic way if you can.

(In other words, please be nice - those who are usually for the most part here - just a bit extra nervous that I can not find what I'm feeling stupid)

method iterative! , which is an example method, is the receiver of self.each_with_index method in itself because the current code is array ( [1,2,3,4] in your example), self is not required; That is, you (and IMO, should) just enter every_in_inquest. N, i | ... . In other words, self is an inherent receiver when no explicit receiver is specified

for your example array = [

  itself [i] = yield (n)  

1,2,3,4] is your counterpart:

  Enum = [1,2,3,4] .each_with_index # = & gt; # & Lt; Anonymator: [1, 2, 3, 4]: each_v_nordore & gt;  

with elements

  enum.to_a # = & gt; [0], [2, 1], [3, 2], [4, 3]]  

in the block aa = every 1,0] , which is assigned to the block variable:

  n = 1 i = 0  

resulting in

  itself [0] = yield (1) = & gt; 1 ** 2 = & gt; 1  

and so on.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -