5

我想@a从:循环020, 1, 2, 0, 1, 2

def set_a
  if @a == 2 
    @a = 0
  else
    @a = @a + 1
  end
end

也许有更好的方法?

4

1 回答 1

20
(0..2).cycle(3) { |x| puts x } #=> 0,1,2,0,1,2,0,1,2

item = [0, 1, 2].cycle.each

item.next #=> 0
item.next #=> 1
item.next #=> 2
item.next #=> 0
...
于 2012-05-29T02:59:34.873 回答