Ruby Language Casting (type conversion) Casting to an Integer

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!


"123.50".to_i     #=> 123
Integer("123.50") #=> 123

A string will take the value of any integer at its start, but will not take integers from anywhere else:

"123-foo".to_i # => 123
"foo-123".to_i # => 0

However, there is a difference when the string is not a valid Integer:

"something".to_i     #=> 0
Integer("something") # ArgumentError: invalid value for Integer(): "something"

Got any Ruby Language Question?