Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Integer#digits

int.digits → array
int.digits(base) → array

Returns the digits of int‘s place-value representation with radix base (default: 10). The digits are returned as an array with the least significant digit as the first array element.

base must be greater than or equal to 2.

Example
12345.digits      #=> [5, 4, 3, 2, 1]
12345.digits(7)   #=> [4, 6, 6, 0, 5]
12345.digits(100) #=> [45, 23, 1]

-12345.digits(7)  #=> Math::DomainError
See also #denominator #div #coerce #divmod also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1