We use type checking to find out what kind of thing (object) we have. This helps us decide what we can do with it.
Type checking with .class and .is_a? in Ruby
Introduction
Syntax
Ruby
object.class
object.is_a?(ClassName).class tells you the exact type of the object.
.is_a? checks if the object is of a type or inherits from that type.
Examples
Ruby
5.class # => Integer
Ruby
"hello".class # => String
Ruby
5.is_a?(Integer) # => true
Ruby
"hello".is_a?(Object) # => true
Sample Program
This program checks the type of different objects and prints messages based on their type.
Ruby
def check_type(obj) puts "Object: #{obj}" puts "Type with .class: #{obj.class}" if obj.is_a?(String) puts "It's a String!" elsif obj.is_a?(Integer) puts "It's an Integer!" else puts "It's something else." end end check_type("Ruby") check_type(42) check_type(3.14)
Important Notes
.class gives the exact class, but .is_a? can check for parent classes too.
Use .is_a? when you want to allow subclasses, not just exact matches.
Type checking helps avoid errors by confirming what you have before using it.
Summary
.class tells you the exact type of an object.
.is_a? checks if an object is a type or inherits from it.
Use these to write safer and clearer code by knowing what you are working with.
Practice
1. What does the
.class method return when called on an object in Ruby?easy
Solution
Step 1: Understand what
The.classdoes.classmethod returns the exact class of the object it is called on.Step 2: Compare with other options
Options A, C, and D describe behaviors of other methods or incorrect meanings.Final Answer:
The exact type (class) of the object -> Option BQuick Check:
.classreturns exact type [OK]
Hint: Remember: .class gives exact type, not inheritance [OK]
Common Mistakes:
- Confusing .class with .is_a?
- Thinking .class returns boolean
- Assuming .class returns parent class
2. Which of the following is the correct syntax to check if variable
x is an instance of the Integer class using .is_a??easy
Solution
Step 1: Recall the syntax of
The method.is_a?.is_a?requires a class argument inside parentheses, like.is_a?(ClassName).Step 2: Check each option
x.is_a?(Integer) uses correct syntax with parentheses and question mark. Options A, B, and D have syntax errors or missing parentheses.Final Answer:
x.is_a?(Integer) -> Option AQuick Check:
Use parentheses with.is_a?[OK]
Hint: Always use parentheses with .is_a? method [OK]
Common Mistakes:
- Omitting parentheses after .is_a?
- Using angle brackets instead of parentheses
- Leaving out the question mark
3. What will be the output of the following Ruby code?
class Animal; end class Dog < Animal; end dog = Dog.new puts dog.class puts dog.is_a?(Animal)
medium
Solution
Step 1: Understand
Thedog.classdogobject is an instance ofDog, sodog.classreturnsDog.Step 2: Understand
Sincedog.is_a?(Animal)Doginherits fromAnimal,dog.is_a?(Animal)returnstrue.Final Answer:
Dog true -> Option AQuick Check:
.classexact type,.is_a?checks inheritance [OK]
Hint: Remember: .class exact, .is_a? true for subclasses too [OK]
Common Mistakes:
- Thinking .class returns parent class
- Assuming .is_a? false for subclasses
- Confusing output order
4. Identify the error in the following Ruby code snippet:
num = 10 if num.is_a?(Integer) puts "Number is integer" else puts "Number is not integer" end
medium
Solution
Step 1: Check
The methodis_a?usage.is_a?is correctly called with parentheses and the classInteger.Step 2: Verify syntax correctness
Theifstatement andputscalls are syntactically correct.Final Answer:
No error, code runs correctly -> Option CQuick Check:
Correct syntax and usage means no error [OK]
Hint: Check parentheses and class names carefully [OK]
Common Mistakes:
- Removing parentheses from is_a?
- Using wrong class name like 'int'
- Misplacing if-end keywords
5. Given the following code, which option correctly filters only strings from the mixed array using
.is_a??
items = ["apple", 42, :symbol, "banana", 3.14]
strings = items.select { |item| ??? }hard
Solution
Step 1: Understand filtering with
We want to select only elements that are strings, so we check if each item is a kind of.is_a?String.Step 2: Compare options
item.is_a?(String) uses.is_a?(String)which returns true for strings. item.class == String uses.class == Stringwhich also works but is less flexible if subclasses exist. Options C and D filter integers or non-strings, which is incorrect.Final Answer:
item.is_a?(String) -> Option DQuick Check:
Use.is_a?(String)to filter strings [OK]
Hint: Use .is_a?(String) to include subclasses too [OK]
Common Mistakes:
- Using .class == String and missing subclasses
- Filtering wrong types by mistake
- Forgetting question mark in .is_a?
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
