0
0
Rubyprogramming~10 mins

Is_a? and kind_of? for type checking in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Is_a? and kind_of? for type checking
Start with object
Call is_a? or kind_of?
Check object's class or ancestors
If matches class or ancestor
YesReturn true
If no match
NoReturn false
The program checks if an object is an instance of a class or its ancestors using is_a? or kind_of?, returning true or false.
Execution Sample
Ruby
num = 5
puts num.is_a?(Integer)
puts num.kind_of?(Numeric)
puts num.is_a?(String)
Check if num is an Integer, Numeric, or String using is_a? and kind_of?.
Execution Table
StepExpressionCheck PerformedResult
1num.is_a?(Integer)Is num an Integer or subclass? (num=5 Integer)true
2num.kind_of?(Numeric)Is num a Numeric or subclass? (Integer < Numeric)true
3num.is_a?(String)Is num a String or subclass?false
💡 All checks done, results returned accordingly.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
num5 (Integer)5 (Integer)5 (Integer)5 (Integer)
Key Moments - 3 Insights
Why do is_a? and kind_of? return true for Numeric when the object is an Integer?
Because Integer is a subclass of Numeric, so the check includes parent classes as shown in step 2 of the execution_table.
Are is_a? and kind_of? different methods?
No, they are aliases and behave the same way, both checking class and ancestors as shown in steps 1 and 2.
Why does is_a? return false for String when the object is an Integer?
Because Integer is not a subclass or instance of String, so the check fails as shown in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of num.is_a?(Integer) at step 1?
Anil
Bfalse
Ctrue
Derror
💡 Hint
Check the 'Result' column in row 1 of execution_table.
At which step does the check confirm num is a Numeric or subclass?
AStep 2
BStep 1
CStep 3
DNone
💡 Hint
Look at the 'Check Performed' column for Numeric in execution_table.
If num was a String instead of Integer, what would num.is_a?(String) return at step 3?
Anil
Btrue
Cfalse
Derror
💡 Hint
Refer to the logic in execution_table step 3 and variable_tracker for num's class.
Concept Snapshot
Use is_a? or kind_of? to check an object's class or ancestors.
Both methods return true if the object is an instance of the class or its parent classes.
Example: 5.is_a?(Integer) is true, 5.kind_of?(Numeric) is true.
They are aliases and behave the same.
Return false if no match is found.
Full Transcript
This visual execution shows how Ruby's is_a? and kind_of? methods check an object's type. We start with an object num set to 5, an Integer. We call num.is_a?(Integer) which returns true because num is an Integer. Then num.kind_of?(Numeric) returns true because Integer is a subclass of Numeric. Finally, num.is_a?(String) returns false because num is not a String. The variable num remains 5 throughout. These methods help check if an object belongs to a class or its ancestors, returning true or false accordingly.