On Jun 25, 2006, at 2:08 PM, Boris Prinz wrote:

> I couldn't resist to add a pair iterator to class Array...

> class Array
>   # iterate through pairs of consecutive elements
>   def each_pair
>     (0..size-2).each do |i|
>       yield(self[i], self[i+1])
>     end
>   end
> end

This is a pretty common idiom covered by the standard library, just FYI:

 >> require "enumerator"
=> true
 >> [1, 3, 3, 1].each_cons(2) do |l, r|
?>   puts "#{l} + #{r} = #{l + r}"
 >> end
1 + 3 = 4
3 + 3 = 6
3 + 1 = 4
=> nil

Thanks for the solution!

James Edward Gray II