Skip to content

Working with Numbers

Last updated on June 4th, 2024 at 06:06 pm

Working with numbers in Ruby is quite easy. The basic rules of the game are:

  • If you assign an integer value to a variable, it will be an integer
  • If you perform math operations on integers, the resulting value will be an integer. Fractions are rounded down.
  • If you assign a floating point value to a variable, it will be a floating point number
  • if you perform math operations on floating point numbers, the resulting value will be a floating point number.
  • If you mix integer and floating point values in assignments or math operations, the result will be floating point.

In Ruby 2.5 the Complex class was added to support complex numbers. For a thorough explanation of how Ruby handles complex numbers, please refer to the Ruby Docs page on this class.