0
0
Rubyprogramming~20 mins

Type checking with .class and .is_a? in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Type Checking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
Output of .class vs .is_a? for Integer
What is the output of the following Ruby code?
Ruby
num = 42
puts num.class
puts num.is_a?(Integer)
A
Integer
true
B
Integer
false
C
Fixnum
true
D
Fixnum
false
Attempts:
2 left
💡 Hint
Remember Ruby 2.4+ unified Fixnum and Bignum into Integer.
Predict Output
intermediate
1:30remaining
Checking class inheritance with .is_a?
What will this Ruby code print?
Ruby
class Animal; end
class Dog < Animal; end
pet = Dog.new
puts pet.class
puts pet.is_a?(Animal)
A
Dog
true
B
Animal
true
C
Dog
false
D
Animal
false
Attempts:
2 left
💡 Hint
Remember that .class returns the object's exact class, but .is_a? checks inheritance.
🔧 Debug
advanced
2:00remaining
Why does this .is_a? check fail?
Consider this Ruby code snippet. Why does the .is_a? check return false?
Ruby
class Vehicle; end
class Car < Vehicle; end
my_car = Vehicle.new
puts my_car.is_a?(Car)
ABecause Car is not defined properly as a subclass
BBecause .is_a? only works with modules, not classes
CBecause my_car is an instance of Vehicle, not Car or its subclass
DBecause .is_a? checks exact class equality, not inheritance
Attempts:
2 left
💡 Hint
Think about the object and class relationship and what .is_a? means.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in type checking
Which option contains a syntax error when checking an object's type in Ruby?
Ruby
obj = 'hello'
Aobj.is_a?(String)
Bobj.is_a?(String))
Cobj.class == String
Dobj.class.eql?(String)
Attempts:
2 left
💡 Hint
Check for unmatched parentheses.
🚀 Application
expert
2:30remaining
Determine the output of complex type checks
What is the output of this Ruby code?
Ruby
module Mixin; end
class Parent; include Mixin; end
class Child < Parent; end
obj = Child.new
puts obj.is_a?(Mixin)
puts obj.class == Parent
puts obj.is_a?(Parent)
A
false
false
true
B
true
true
true
C
true
false
false
D
true
false
true
Attempts:
2 left
💡 Hint
Remember that .is_a? checks inheritance and included modules, but .class checks exact class.