Possible duplicate:Safe One-Piece Parsing in Ruby
int.Parse converts the string to an integer, but throws an exception if the string cannot be converted. int.TryParse does not int.TryParse error when it cannot convert sting to int, but returns 0 and bool , which say whether the string can be converted.
int.Parse
int.TryParse
0
bool
Is there something similar in Ruby?
There is no direct equivalent in Ruby. Two main options:
Integer('42')
Int32.Parse
String.to_i
"42".to_i
The integer (str) is probably Integer('123') you need - Integer('123') will return 123, and Integer('123a') throw an exception.
Integer('123')
Integer('123a')