Bird
Raised Fist0
Rubyprogramming~10 mins

Integer and Float number types in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Integer and Float number types
Start
↓
Define number
↓
Is number whole?
Yes→Integer type
↓
Use integer
↓
Float type
↓
End
↓
Use float
↓
End
The program checks if a number is whole or has decimals, then treats it as Integer or Float accordingly.
Execution Sample
Ruby
a = 5
b = 3.14
puts a.class
puts b.class
This code defines an integer and a float, then prints their types.
Execution Table
StepActionVariableValueOutput
1Assign 5 to aa5
2Assign 3.14 to bb3.14
3Check class of aa.classInteger
4Print class of aInteger
5Check class of bb.classFloat
6Print class of bFloat
💡 All assignments and prints done, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
aundefined555
bundefinedundefined3.143.14
Key Moments - 2 Insights
Why does 5 have class Integer but 3.14 has class Float?
Because 5 is a whole number without decimals, Ruby treats it as Integer (see step 3). 3.14 has decimals, so Ruby treats it as Float (see step 5).
What happens if I assign 5.0 instead of 5?
5.0 has decimals, so Ruby treats it as Float, not Integer. It changes the class from Integer to Float.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the class of variable 'a' at step 3?
AInteger
BFloat
CString
DUndefined
💡 Hint
Check the 'Variable' and 'Value' columns at step 3 in the execution_table.
At which step does the program print 'Float'?
AStep 4
BStep 6
CStep 5
DStep 2
💡 Hint
Look at the 'Output' column for the print statements in the execution_table.
If variable 'b' was assigned 7 instead of 3.14, what would be its class?
AFloat
BString
CInteger
DBoolean
💡 Hint
Refer to the variable_tracker and key_moments about how Ruby treats whole numbers.
Concept Snapshot
Integer and Float in Ruby:
- Integer: whole numbers without decimals (e.g., 5)
- Float: numbers with decimals (e.g., 3.14)
- Use .class to check type
- Ruby treats 5 as Integer, 5.0 as Float
- Important for math and memory use
Full Transcript
This lesson shows how Ruby handles two main number types: Integer and Float. Integers are whole numbers like 5, while Floats have decimals like 3.14. The code example assigns 5 to variable 'a' and 3.14 to 'b'. Using .class, we see 'a' is Integer and 'b' is Float. This helps Ruby know how to store and calculate with these numbers. If you assign 5.0 instead of 5, Ruby treats it as Float because of the decimal. Understanding this helps avoid confusion when working with numbers in Ruby.

Practice

(1/5)
1. Which of the following is an example of an Integer in Ruby?
easy
A. "100"
B. 3.14
C. true
D. 42

Solution

  1. Step 1: Understand Integer definition

    An Integer is a whole number without any decimal part.
  2. Step 2: Identify the correct option

    42 is a whole number, so it is an Integer. 3.14 has decimals (Float), "100" is a string, and true is a boolean.
  3. Final Answer:

    42 -> Option D
  4. Quick Check:

    Integer = 42 [OK]
Hint: Integers have no decimal point, floats do. [OK]
Common Mistakes:
  • Confusing strings with numbers
  • Thinking floats are integers
  • Mixing booleans with numbers
2. Which of the following is the correct way to write a float number in Ruby?
easy
A. 5.5
B. 5-5
C. 5..5
D. 5,5

Solution

  1. Step 1: Recall float syntax in Ruby

    Floats use a dot (.) to separate the whole number and decimal parts.
  2. Step 2: Check each option

    5.5 uses a dot correctly. 5,5 uses a comma which is invalid. 5..5 is a range syntax, not a float. 5-5 is subtraction.
  3. Final Answer:

    5.5 -> Option A
  4. Quick Check:

    Float uses dot like 5.5 [OK]
Hint: Floats always use a dot, never a comma. [OK]
Common Mistakes:
  • Using commas instead of dots
  • Confusing ranges with floats
  • Using operators instead of decimal points
3. What will be the output of this Ruby code?
num1 = 10
num2 = 3.0
result = num1 / num2
puts result
medium
A. 3.0
B. 3
C. 3.3333333333333335
D. 3.333

Solution

  1. Step 1: Understand division with Integer and Float

    Dividing an Integer by a Float returns a Float result in Ruby.
  2. Step 2: Calculate 10 / 3.0

    10 divided by 3.0 equals approximately 3.3333333333333335 as a Float.
  3. Final Answer:

    3.3333333333333335 -> Option C
  4. Quick Check:

    Integer / Float = Float result [OK]
Hint: Integer divided by float gives float result. [OK]
Common Mistakes:
  • Expecting integer division truncation
  • Rounding the float result incorrectly
  • Confusing float with string output
4. Find the error in this Ruby code snippet:
num = 7.5
puts num.to_i.to_f
medium
A. SyntaxError due to method chaining
B. No error, output is 7.0
C. RuntimeError because to_i cannot be chained
D. Output is 7.5, no conversion happened

Solution

  1. Step 1: Understand method chaining on numbers

    Ruby allows chaining methods like to_i and to_f on numbers without error.
  2. Step 2: Evaluate the code output

    num.to_i converts 7.5 to 7 (integer), then to_f converts 7 to 7.0 (float). So output is 7.0.
  3. Final Answer:

    No error, output is 7.0 -> Option B
  4. Quick Check:

    Method chaining works, output 7.0 [OK]
Hint: to_i then to_f converts float to integer then float again. [OK]
Common Mistakes:
  • Thinking method chaining causes syntax error
  • Expecting original float output
  • Confusing to_i and to_f effects
5. You want to calculate the average of two numbers in Ruby, where both are Integers. Which code correctly calculates the average as a Float?
a = 8
b = 5
average = ?
hard
A. (a + b) / 2.0
B. (a + b) / 2
C. (a.to_i + b.to_i) / 2
D. a / 2 + b / 2

Solution

  1. Step 1: Understand division with integers and floats

    Dividing an Integer truncates if both operands are integers; dividing by a Float keeps decimal.
  2. Step 2: Check each option for correct average calculation

    (a + b) / 2: 13 (Integer) / 2 (Integer) = 6 (truncated).
    (a + b) / 2.0: 13 / 2.0 = 6.5 (Float).
    (a.to_i + b.to_i) / 2: forces Integers, result 6 (Integer).
    a / 2 + b / 2: 8/2 + 5/2 = 4 + 2 = 6 (Integer divisions).
  3. Final Answer:

    (a + b) / 2.0 -> Option A
  4. Quick Check:

    Divide sum by float 2.0 for float average [OK]
Hint: Divide by 2.0 to keep float result for average. [OK]
Common Mistakes:
  • Dividing by integer causing integer division
  • Converting floats to integers losing decimals
  • Not using float divisor for average
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard