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
numeric.c
View on GitHub ↗
static VALUE
rb_int_digits(int argc, VALUE *argv, VALUE num)
{
VALUE base_value;
long base;
if (rb_num_negative_p(num))
rb_raise(rb_eMathDomainError, "out of domain");
if (rb_check_arity(argc, 0, 1)) {
base_value = rb_to_int(argv[0]);
if (!RB_INTEGER_TYPE_P(base_value))
rb_raise(rb_eTypeError, "wrong argument type %s (expected Integer)",
rb_obj_classname(argv[0]));
if (RB_TYPE_P(base_value, T_BIGNUM))
return rb_int_digits_bigbase(num, base_value);
base = FIX2LONG(base_value);
if (base < 0)
rb_raise(rb_eArgError, "negative radix");
else if (base < 2)
rb_raise(rb_eArgError, "invalid radix %ld", base);
}
else
base = 10;
if (FIXNUM_P(num))
return rb_fix_digits(num, base);
else if (RB_TYPE_P(num, T_BIGNUM))
return rb_int_digits_bigbase(num, LONG2FIX(base));
return Qnil;
}
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