concept Float in category ruby

appears as: Float, Float, The Float
The Well-Grounded Rubyist, Third Edition

This is an excerpt from Manning's book The Well-Grounded Rubyist, Third Edition.

Array, Hash, String, and Float are all built-in classes because they’re useful and prevalent go-tos available without so much as a require statement. By contrast, Date is not a built-in class because one must require 'date' before using it.

Stricter conversions with Integer and Float

Ruby provides methods called Integer and Float (and yes, they look like constants, but they’re methods with names that coincide with those of the classes to which they convert). These methods are similar to to_i and to_f, respectively, but a little stricter: if you feed them anything that doesn’t conform to the conversion target type, they raise an exception:

>> "123abc".to_i
=> 123
>> Integer("123abc")
ArgumentError: invalid value for Integer(): "123abc"
>> Float("3")
=> 3.0
>> Float("-3")
=> -3.0
>> Float("-3xyz")
ArgumentError: invalid value for Float(): "-3xyz"