Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Numeric#coerce

num.coerce(numeric) → array

If numeric is the same type as num, returns an array [numeric, num]. Otherwise, returns an array with both numeric and num represented as Float objects.

This coercion mechanism is used by Ruby to handle mixed-type numeric operations: it is intended to find a compatible common type between the two operands of the operator.

Example
1.coerce(2.5)   #=> [2.5, 1.0]
1.2.coerce(3)   #=> [3.0, 1.2]
1.coerce(2)     #=> [2, 1]
See also #clone #conj #ceil #conjugate also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1