Numbers

Integers

Integers are whole numbers: counts, ages, and indexes. They can be positive, negative, or zero, and Ruby handles very large integers without extra effort.

Underscores may split long digits into groups of three. They are ignored by the interpreter and exist only for human eyes:

seats = 42 crowd = 1_000_000 puts seats # prints 42 puts crowd # prints 1000000

Floats

Floats are numbers with a decimal point: prices, weights, and averages. Any number written with a dot becomes a float, even when the fraction is zero.

price = 9.99 whole = 3.0 puts price # prints 9.99 puts whole # prints 3.0

Basic math

The usual operators work as expected: plus, minus, multiply, divide, remainder, and power. Watch out for one detail: dividing two integers floors the result to a whole number, so 7 / 2 is 3 and -7 / 2 is -4.

puts 2 + 3 # prints 5 puts 2 ** 10 # prints 1024 puts 7 / 2 # prints 3 puts 7.0 / 2 # prints 3.5

ruby math.rb

5
1024
3
3.5

When at least one side is a float, the result is a float. That is the simplest way to keep the decimal part of a division.

To_i to_f round

Input from users and files arrives as strings, so Ruby offers small converters with clear names. to_i turns a string into an integer, to_f into a float, and round trims a float to the nearest whole number.

puts "42".to_i # prints 42 puts "3.5".to_f # prints 3.5 puts 3.7.round # prints 4 puts 3.2.round # prints 3

ruby convert.rb

42
3.5
4
3

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does 1_000_000 mean to Ruby?

    Answer

    The integer 1000000

    Underscores inside a number are ignored, so they only group the digits for a human reader.

  2. What does this program print? puts 7 / 2

    Answer

    3

    Both sides are integers, so the division floors the result to 3.

  3. What does this program print? puts -7 / 2

    Answer

    -4

    Integer division floors towards minus infinity, so -3.5 becomes -4.

  4. What does this program print? puts 7.0 / 2

    Answer

    3.5

    One float on either side makes the result a float, so the decimal part stays.

  5. Which of these numbers is a Float?

    Answer

    3.0

    The dot makes a Float even when the fraction is zero; 3 and 0x10 are Integers.

  6. What does "3.5".to_i return?

    Answer

    3

    to_i reads the digits and stops at the dot, so the fraction is dropped without rounding.

  7. What does this program print? puts 2 ** 10

    Answer

    1024

    The ** operator raises the left number to the power of the right one.

  8. What does this program print? puts 3.7.round puts 3.2.round

    Answer

    4 / 3

    round gives the nearest whole number, so 3.7 goes up and 3.2 goes down.

  9. Why does puts "42".to_i + 1 print 43?

    Answer

    Because to_i turns the text into the integer 42 first

    to_i returns an Integer, and the addition of two Integers is arithmetic.

  10. What type is the result of 4 + 2.0?

    Answer

    Float

    Mixing an Integer with a Float promotes the result to Float, 6.0.

Article author: Arthur Isaev