ruby - Iterate over loop accessing two elements if they exist -
well, haven't found clean solution write code in ruby:
# java style version: array.each |i, el| if < array.length - 1 process(array[i], array[i+1]) end end # nice if this: array.each |i, el, next| process(el, next) end
you can use each_cons
:
array.each_cons(2) |a, b| process(a, b) end
Comments
Post a Comment