to_i(...)
public
Returns the result of interpreting leading characters in str as an
integer base base (between 2 and 36). Extraneous characters past
the end of a valid number are ignored. If there is not a valid number at
the start of str, 0 is returned. This method never raises an
exception.
"12345".to_i
"99 red balloons".to_i
"0a".to_i
"0a".to_i(16)
"hello".to_i
"1100101".to_i(2)
"1100101".to_i(8)
"1100101".to_i(10)
"1100101".to_i(16)
Show source
/*
* call-seq:
* str.to_i(base=10) => integer
*
* Returns the result of interpreting leading characters in <i>str</i> as an
* integer base <i>base</i> (between 2 and 36). Extraneous characters past the
* end of a valid number are ignored. If there is not a valid number at the
* start of <i>str</i>, <code>0</code> is returned. This method never raises an
* exception.
*
* "12345".to_i #=> 12345
* "99 red balloons".to_i #=> 99
* "0a".to_i #=> 0
* "0a".to_i(16) #=> 10
* "hello".to_i #=> 0
* "1100101".to_i(2) #=> 101
* "1100101".to_i(8) #=> 294977
* "1100101".to_i(10) #=> 1100101
* "1100101".to_i(16) #=> 17826049
*/
static VALUE
rb_str_to_i(argc, argv, str)
int argc;
VALUE *argv;
VALUE str;
{
VALUE b;
int base;
rb_scan_args(argc, argv, "01", &b);
if (argc == 0) base = 10;
else base = NUM2INT(b);
if (base < 0) {
rb_raise(rb_eArgError, "illegal radix %d", base);
}
return rb_str_to_inum(str, base, Qfalse);
}