From: Rick DeNatale Date: 2007-05-01T05:10:29+09:00 Subject: Re: cyclic array On 4/30/07, Xavier Noria wrote: > On Apr 30, 2007, at 3:43 AM, Rick DeNatale wrote: > > > On 4/28/07, Xavier Noria wrote: > > > >> > >> This is a possible approach: > >> > >> module Enumerable > >> def each_cycle(window, start=0) > >> (start...length+start).each do |i| > >> yield((i..i+window).map {|n| self[n % length]}) > >> end > >> end > >> end > > > > This method won't work for Enumerables in general, although I guess it > > does work for Arrays. > > > > Not all enumerables have one or both length or [] methods. > > Oh right, thank you. I don't know why ri Enumerable lists length > here, looks like it is getting non-core methods from somewhere else. > I overlooked [] though. The OP wanted this for an Array, it would > certainly work there. By the way I think that there's a typo that should be an open range in the yield expression, otherwise you get yield((i...i+window).map {|n| self[n % length]}) Thinking about the Enumerable vs. Array aspect of this got me thinking abou the subtleties of duck typing leading to another blog entry: http://talklikeaduck.denhaven2.com/articles/2007/04/30/our-kind-of-ducks-odd-ducks-and-trained-ducks -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/