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

Float#coerce

coerce(other) → array

Returns a 2-element array containing other converted to a Float and self:

Example
f = 3.14                 # => 3.14
f.coerce(2)              # => [2.0, 3.14]
f.coerce(2.0)            # => [2.0, 3.14]
f.coerce(Rational(1, 2)) # => [0.5, 3.14]
f.coerce(Complex(1, 0))  # => [1.0, 3.14]

Raises an exception if a type conversion fails.

See also #ceil #denominator #arg #divmod also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0