Hi gurus and nubys,
Suppose you are dealing with an array like [1,2,3,4], and suppose ypou
want to deal, at any iteration, with 3 values.I mean something like:
[1,2,3,4].each_3 do |pre,cur,next|
print pre,cur,next,"\n"
end
nil,nil,1
nil,1,2
1,2,3
2,3,4
3,4,nil
4,nil,nil
What do you think is the best way to this? I can think of adding a
global index and use it to refer explicitly to an element oin the
array, but this is ugly to me…
Another way would be some kind of subversion of inject(), cause for
just two values it can be used to do what I want:
[1,2,3,4,5,6].inject do |pre1,cur|
print pre1,cur,"\n"
cur
end
I thought this should work:
[1,2,3,4,5,6].inject do |pre2,cur|
print pre2[0],pre2[1],cur,"\n"
[pre2[1],cur]
end
but it gives strange results in the first loops.
102
023
234
345
456
BTW, the point of this mail is: what do you think is the most rubyish
way to handle multiple values at a time ?