ruby - Is there special variable represent array itself in a each block for Enumerable? -


i have code this:

array = [100, 90, 120, 100, 110] array.each_with_index.map |v, i|     next if == array.size - 1     array[i+1] - v end 

and thought nicer if can write this:

array = [100, 90, 120, 100, 110] array.each_with_index.map |v, i|     next if == _.size - 1     _[i+1] - v end 

so want know if there special variable represent array in enumerator block.

does know that?

ruby has block scope, meaning, new variable define inside do/end block in scope , not accessible anywhere else. in irb, _ has special meaning, value of last expression executed. isn't case when executing ruby program, however. can define temporary _ variable inside block hold reference array:

arr = [1,2,3] arr.each |x|   _ = arr   p _ #=> print [1,2,3] 3 times end  p _ #=> undefined local variable 

other that, i'd consider monkey-patching array that's risky operation. of times questions these, however, if give better context of you're trying achieve, there's simpler solution hacks these.


Comments

Popular posts from this blog

how to display 2 form fields on same line with bootstrap -

How do you convert a timestamp into a datetime in python with the correct timezone? -