Question: I want to program a counter which is represented by an array of numbers, starting with:

```
[0, 0, 0]

```

The constraint here is, that each position has a different cap, so it's not necessarily 9 or something else, but it is given. For instance:

```
[4, 2, 1]

```

Which would lead to the following incrementation sequence:

```
[0, 0, 0]
[0, 0, 1] 
[0, 1, 0]
[0, 1, 1]
[0, 2, 0]
[0, 2, 1]
[1, 0, 0]
.
.
.

```

Of course I can think of a solution using modulo and adding each carryover onto the next position. But has someone an idea how to implement this efficiently, respectively with nice Ruby syntax without cluttering it too much?

That is my naive implementation:

```
max = [10, 1, 1, 1, 10]
counter = [0, 0, 0, 0, 0]

i = counter.length-1
while counter != max do
   counter[i] = counter[i] + 1
   while counter[i] > max[i]
      counter[i] = 0
      i = i - 1
      counter[i] = counter[i] + 1
   end
   i = counter.length-1
 end

```

Answer: 
Edit: I was writing this before you made your edit. It seemed like you wanted a counter object, not just to output a list.

1) I would recommend specifying not the limits but (limit+1) of each of the digits. For example, for a [second, minute, hour, day, year] counter it makes more sense (to me) to write [60, 60, 24, 365] instead of [59,59,23,364].

2) You'll have to figure out what to do if your counter overflows the last limit of your array. I added an extra position that counts to infinity.

3) I would also recommend reversing the order of the array, at least in the internal representation to avoid inverting subscripts. If you don't want it like that, you can `.reverse` the `bases` in `initialize` and `@digits` in `to_s`

```
class MyCounter 
  def initialize bases
    @bases = bases
    @bases << 1.0/0 # Infinity
    @digits = Array.new(bases.size, 0)
    prod = 1
    @digit_values = [1] + @bases[0..-2].map { |b| prod *= b }
  end

  attr_reader :digit_values

  def to_s
    @digits
  end

  def increment(digit=0)
    v = @digits[digit] + 1
    if v < @bases[digit]
      @digits[digit] = v
    else 
      @digits[digit] = 0
      increment(digit+1)
    end
    self
  end

  def +(integer)
    (@digits.size - 1).step(0,-1).each do |i|
      @digits[i] += integer / @digit_values[i]
      integer = integer % @digit_values[i]
    end
    self
  end
end

c1 = MyCounter.new [2,3,5]
20.times { c1.increment; p c1 }

c2 = MyCounter.new [2,3,5]
c2 += 20
p c2

```
Create an array for each cap, with values from 0 upto cap. Take the first array and calculate the Cartesian product with the rest of the arrays.

```
caps = [4, 2, 1]
arrs = caps.map{|cap| (0..cap).to_a} #=>[[0, 1, 2, 3, 4], [0, 1, 2], [0, 1]]
p arrs.shift.product(*arrs)
# =>[[0, 0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1], [0, 2, 0], [0, 2, 1], ...

```

If you don't want a memory-consuming array with the results, then provide a block. `product` will yield each element to it, one by one.

```
arrs = caps.map{|cap| (0..cap).to_a}
arrs.shift.product(*arrs){|el| puts el.join} #no resulting array
#000
#001
#010
#011
#...

```