Procs From "Understanding Ruby Blocks, Procs, and Lambdas" Article -
OK, through this review of process, lumber, and block.
Question on this code:
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 1,2,3,4] array = [
is your counterpart:
itself [i] = yield (n)
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
Post a Comment