num = 42 puts num.class puts num.is_a?(Integer)
The .class method returns the exact class of the object, which is Integer in modern Ruby versions.
The .is_a?(Integer) method checks if the object is an instance of Integer or its subclasses, so it returns true.
class Animal; end class Dog < Animal; end pet = Dog.new puts pet.class puts pet.is_a?(Animal)
pet.class returns Dog because pet is an instance of Dog.
pet.is_a?(Animal) returns true because Dog inherits from Animal.
class Vehicle; end class Car < Vehicle; end my_car = Vehicle.new puts my_car.is_a?(Car)
my_car is an instance of Vehicle, not Car. The .is_a?(Car) method returns true only if the object is an instance of Car or its subclasses.
obj = 'hello'Option B has an extra closing parenthesis, causing a syntax error.
Other options are syntactically correct ways to check type.
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)
obj.is_a?(Mixin) is true because Parent includes Mixin and Child inherits Parent.
obj.class == Parent is false because obj's class is Child, not Parent.
obj.is_a?(Parent) is true because Child inherits Parent.