Ruby: Convert to Integer

To convert a string or other object into an integer (number) do this:

"23".to_i

This will produce 23 (as a number)

If you do something like this:

"Hello".to_i

You will get 0 returned, because Ruby can’t understand that object and translate it into a number.

, , , ,

Leave a Reply

You must be logged in to post a comment.