On Apr 28, 7:58 am, Phrogz <g...@refinery.com> wrote:
> On Apr 28, 7:05 am, Josselin <josse...@wanadoo.fr> wrote:
>
> > I would like to print n elements from an Array in a cyclic way.
>
> Not a direct answer to your question, but regarding circular lists
> (which can be created from an array):
> http://phrogz.net/RubyLibs/rdoc/files/Ouroboros_rb.html
Expanding on its usage for the stated problem:
irb(main):001:0> require 'Ouroboros.rb'
=> true
irb(main):002:0> anArray = [ "a", "b", "c", "d", "e", "f", "g"]
=> ["a", "b", "c", "d", "e", "f", "g"]
irb(main):003:0> aSnake = Ouroboros.from_a anArray
=> #<Ouroboros:0x367810 @current_index=0, @current="a", @all=["a",
"b", "c", "d", "e", "f", "g"], @size=7>
irb(main):004:0> aSnake.to_a[ 0...4 ]
=> ["a", "b", "c", "d"]
irb(main):005:0> aSnake.increment
=> ["b"]
irb(main):006:0> aSnake.to_a[ 0...4 ]
=> ["b", "c", "d", "e"]
irb(main):008:0> aSnake.increment
=> "c"
irb(main):009:0> aSnake.to_a[ 0...4 ]
=> ["c", "d", "e", "f"]
....and so on.