Challenge - 5 Problems
Ruby Type Checker Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember that is_a? returns true if the object is an instance of the class or its subclass.
✗ Incorrect
The Dog class inherits from Animal. So, fido.is_a?(Animal) returns true because fido is an instance of Dog, which is a subclass of Animal.
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
kind_of? behaves like is_a? and checks inheritance.
✗ Incorrect
arr is an instance of Array, so arr.kind_of?(Array) is true. Since Array inherits from Object, arr.kind_of?(Object) is also true.
❓ Predict Output
advanced2: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)
Attempts:
2 left
💡 Hint
Both methods check the same inheritance chain.
✗ Incorrect
In Ruby, is_a? and kind_of? are synonyms. Both return true if the object is an instance of the class or its subclass.
❓ Predict Output
advanced2: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)
Attempts:
2 left
💡 Hint
is_a? returns true if the object includes the module.
✗ Incorrect
The Bird class includes the Flyable module. So, sparrow.is_a?(Flyable) returns true.
❓ Predict Output
expert3: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)
Attempts:
2 left
💡 Hint
Singleton class does not change the object's class type.
✗ Incorrect
The object obj is still an instance of Object, so obj.is_a?(Object) is true. It is not an instance of Class, so obj.is_a?(Class) is false.