#!/usr/bin/env ruby # This code comes from http://ruby-it.org/pages/Iterare+Sequenze # Check the page for copyright notice and explanations >> count=0 => 0 >> ary= [1,2,3,4,5,6,7,8] => [1, 2, 3, 4, 5, 6, 7, 8] >> for i in ary >> print ary[count-1],i,ary[count+1],"\n" >> count+=1 >> end 812 123 234 345 456 567 678 78nil => [1, 2, 3, 4, 5, 6, 7, 8] >> ary.each_with_index do |el,i| ?> print ary[i-1],el,ary[i+1],"\n" >> end 812 123 234 345 456 567 678 78nil => [1, 2, 3, 4, 5, 6, 7, 8] >> require 'enumerator' => true >> ary.each_cons(3) do |pre,el,suc| ?> print pre,el,suc,"\n" >> end 123 234 345 456 567 678 => nil