Ruby Newbie homepage Ruby Newbie homepage

How to use

Quick guide

Official content
Returns the result of interpreting leading characters in self as an integer in the given base (which must be in (2..36)):
'123456'.to_i     # => 123456
'123def'.to_i(16) # => 1195503
Characters past a leading valid number (in the given base) are ignored:
'12.345'.to_i   # => 12
'12345'.to_i(2) # => 1
Returns zero if there is no leading valid number:
'abcdef'.to_i # => 0
'2'.to_i(2)   # => 0
 
               static VALUE
rb_str_to_i(int argc, VALUE *argv, VALUE str)
{
    int base = 10;

    if (rb_check_arity(argc, 0, 1) && (base = NUM2INT(argv[0])) < 0) {
        rb_raise(rb_eArgError, "invalid radix %d", base);
    }
    return rb_str_to_inum(str, base, FALSE);
}
            

Was this page useful?