Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Integer#digits

digits(base = 10) → array_of_integers

Returns an array of integers representing the base-radix digits of self; the first element of the array represents the least significant digit:

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

Raises an exception if self is negative or base is less than 2.

See also #denominator #div #coerce #divmod also in 4.1 · 4.0 · 3.4 · 3.2 · 3.1 · 3.0