📖 Scenario: Imagine you are building a simple program that handles different types of items in a store. You want to check the type of each item to decide how to process it.
🎯 Goal: You will create a list of items with different types, set a type to check for, use .class and .is_a? methods to check item types, and print the results.
📋 What You'll Learn
Create an array called items with exact elements: 42, "apple", 3.14, :symbol, and [1, 2, 3]
Create a variable called type_to_check and set it to String
Use a for loop with variable item to iterate over items
Inside the loop, use item.class == type_to_check to check type and store result in class_check
Inside the loop, use item.is_a?(type_to_check) to check type and store result in is_a_check
Print the item, class_check, and is_a_check in each loop iteration
💡 Why This Matters
🌍 Real World
Type checking helps programs decide how to handle different kinds of data, like numbers, text, or lists, which is common in many software applications.
💼 Career
Understanding type checking is important for debugging, writing safe code, and working with object-oriented programming in Ruby and other languages.
Progress0 / 4 steps
1
Create the items array
Create an array called items with these exact elements: 42, "apple", 3.14, :symbol, and [1, 2, 3]
Ruby
Hint
Use square brackets [] to create an array and separate elements with commas.
2
Set the type_to_check variable
Create a variable called type_to_check and set it to the class String
Ruby
Hint
Assign the class String directly to the variable.
3
Use a for loop to check types
Use a for loop with variable item to iterate over items. Inside the loop, use item.class == type_to_check to check type and store the result in class_check. Also use item.is_a?(type_to_check) to check type and store the result in is_a_check
Ruby
Hint
Use parentheses around item.class == type_to_check to store the boolean result.
4
Print the results inside the loop
Inside the for loop, print the item, class_check, and is_a_check values in each iteration using puts
Ruby
Hint
Use string interpolation with #{} inside puts to show values.
Practice
(1/5)
1. What does the .class method return when called on an object in Ruby?
easy
A. Whether the object is a kind of a class
B. The exact type (class) of the object
C. The parent class of the object
D. A boolean indicating if the object is a class
Solution
Step 1: Understand what .class does
The .class method 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 B
Quick Check:
.class returns 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
A. x.is_a?(Integer)
B. x.is_a(Integer)
C. x.is_a<Integer>
D. x.is_a? Integer
Solution
Step 1: Recall the syntax of .is_a?
The method .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 A
Quick 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
A. Dog
true
B. Animal
true
C. Dog
false
D. Animal
false
Solution
Step 1: Understand dog.class
The dog object is an instance of Dog, so dog.class returns Dog.
Step 2: Understand dog.is_a?(Animal)
Since Dog inherits from Animal, dog.is_a?(Animal) returns true.
Final Answer:
Dog
true -> Option A
Quick Check:
.class exact 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
A. Syntax error in if statement
B. Missing parentheses in is_a? call
C. No error, code runs correctly
D. Incorrect class name Integer
Solution
Step 1: Check is_a? usage
The method .is_a? is correctly called with parentheses and the class Integer.
Step 2: Verify syntax correctness
The if statement and puts calls are syntactically correct.
Final Answer:
No error, code runs correctly -> Option C
Quick 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??
We want to select only elements that are strings, so we check if each item is a kind of String.
Step 2: Compare options
item.is_a?(String) uses .is_a?(String) which returns true for strings. item.class == String uses .class == String which 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 D
Quick 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 unknownstrongwhile earning +1 Grip toward your certificate.