Turning an Integer into an array of ints My goal here is pretty simple, I want to turn a number like 12435987234 into an array of integers in reverse order.  So that would look like:

[4,3,2,7,8,9,5,3,4,2,1]


I want to do this without changing the number to a string and back.  Really, I don't want to convert it at all but I'm open to hearing ideas around this.

I've found one way to do in ruby via #divmod but suspect there is a more perfomant way.

# turns 1234 into
# [4,3,2,1]
def reversed_digits(val)
  quot = val
  results = []
  while quot &gt; 0 do
    quot, remainder = quot.divmod(10)
    results &lt;&lt; remainder
  end
  results
end

puts reversed_digits(1234)
=&gt; [4,3,2,1]
I don't think your algorithm can be improved in speed. I'd propose a different implementation, though, a more shorter recursive function:

def reversed_digits(num, base: 10)
  quotient, remainder = num.divmod(base)
  [remainder] + (quotient == 0 ? [] : reversed_digits(quotient, base: base))
end