0
0
Rubyprogramming~20 mins

Is_a? and kind_of? for type checking in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Type Checker Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of is_a? with inheritance
What is the output of this Ruby code snippet?
Ruby
class Animal; end
class Dog < Animal; end

fido = Dog.new
puts fido.is_a?(Animal)
Atrue
Bfalse
Cnil
DTypeError
Attempts:
2 left
💡 Hint
Remember that is_a? returns true if the object is an instance of the class or its subclass.
Predict Output
intermediate
2:00remaining
kind_of? method behavior
What will this Ruby code print?
Ruby
arr = [1, 2, 3]
puts arr.kind_of?(Array)
puts arr.kind_of?(Object)
Atrue\nfalse
Btrue\ntrue
Cfalse\ntrue
Dfalse\nfalse
Attempts:
2 left
💡 Hint
kind_of? behaves like is_a? and checks inheritance.
Predict Output
advanced
2:00remaining
Difference between is_a? and kind_of?
What is the output of this Ruby code?
Ruby
class Vehicle; end
class Car < Vehicle; end

car = Car.new
puts car.is_a?(Vehicle)
puts car.kind_of?(Vehicle)
Atrue\ntrue
Btrue\nfalse
Cfalse\ntrue
Dfalse\nfalse
Attempts:
2 left
💡 Hint
Both methods check the same inheritance chain.
Predict Output
advanced
2:00remaining
Checking type with is_a? for modules
What will this Ruby code output?
Ruby
module Flyable; end
class Bird
  include Flyable
end

sparrow = Bird.new
puts sparrow.is_a?(Flyable)
ANoMethodError
Bfalse
Ctrue
DTypeError
Attempts:
2 left
💡 Hint
is_a? returns true if the object includes the module.
Predict Output
expert
3:00remaining
Output of is_a? with singleton class
What does this Ruby code print?
Ruby
obj = Object.new
class << obj
  def greet; 'hello'; end
end

puts obj.is_a?(Object)
puts obj.is_a?(Class)
Atrue\ntrue
Bfalse\nfalse
Cfalse\ntrue
Dtrue\nfalse
Attempts:
2 left
💡 Hint
Singleton class does not change the object's class type.