Rubyで列挙子をループバックする方法はありますか? このコードを考えると:
a=[1,2,3]
a.to_enum
a.next => 1
a.next => 2
a.next => 3
a.next => 1
next列挙子が最後の要素に到達したときに、メソッドを最初の要素に戻すにはどうすればよいですか?
Rubyで列挙子をループバックする方法はありますか? このコードを考えると:
a=[1,2,3]
a.to_enum
a.next => 1
a.next => 2
a.next => 3
a.next => 1
next列挙子が最後の要素に到達したときに、メソッドを最初の要素に戻すにはどうすればよいですか?
使用できますEnumerable#cycle:
a = [1, 2, 3]
enum = a.cycle  #=> #<Enumerator: [1, 2, 3]:cycle>
enum.next       #=> 1
enum.next       #=> 2
enum.next       #=> 3
enum.next       #=> 1
    Enumerator.html#rewindを使用することもできますrewind 
a.rewind
少し前に尋ねたのとまったく同じ質問how-to-point-to-first-element-when-object-next-reached-the-end