Skip to content

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

instance method

String#to_i

to_i(base = 10) → integer

Returns the result of interpreting leading characters in self as an integer in the given base (which must be in (2..36)):

Example
'123456'.to_i     # => 123456
'123def'.to_i(16) # => 1195503

Characters past a leading valid number (in the given base) are ignored:

Example
'12.345'.to_i   # => 12
'12345'.to_i(2) # => 1

Returns zero if there is no leading valid number:

Example
'abcdef'.to_i # => 0
'2'.to_i(2)   # => 0
See also #to_f #tojis #toeuc #tolocale also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0