0
0
Rubyprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Integer and Float number types
Start
Define number
Is number whole?
YesInteger 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.