From: Harry Date: 2007-04-28T22:36:55+09:00 Subject: Re: cyclic array ------=_Part_57334_30932714.1177767413219 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 4/28/07, Josselin wrote: > > I would like to print n elements from an Array in a cyclic way. > > I mean : > > using anArray = [ "a", "b", "c", "d", "e", "f", "g"], I should do : > > anArray.print_cyclic(4, 0) => "a", "b", "c", "d" (first print 4 > elements starting with the first one) > > anArray.print_cyclic_next => "b", "c", "d", "e" > anArray.print_cyclic_next => "c", "d", "e", "f" > anArray.print_cyclic_next => "d", "e", "f", "g" > anArray.print_cyclic_next => "e", "f", "g", "a" > anArray.print_cyclic_next => "f", "g", "a", "b" > anArray.print_cyclic_next => "g", "a", "b", "c" > ..... > also in reverse cycle > anArray.print_cyclic(4, 0) => "a", "b", "c", "d" > anArray.print_cyclic_previous => "g", "a", "b", "c" > anArray.print_cyclic_previous => "f", "g", "a", "b", > > what could be the simplest way to do it ? no simple way using Array > class methods only , > should I define a class and methods to loop into the array ? any > existing lib ? if it has been already done why should I rewrite it .... > > > tfyh > > joss > > > This will do the shifting but I'm not sure about wrapping around. require 'enumerator' (1..25).each_cons(5) do |x| p x end Harry -- http://www.kakueki.com/ruby/list.html A Look into Japanese Ruby List in English ------=_Part_57334_30932714.1177767413219--