Complete the code to check if the variable obj is a String using is_a?.
obj = "hello" puts obj.[1](String)
The is_a? method checks if an object is an instance of a class or its subclass. Here, obj.is_a?(String) returns true because obj is a String.
Complete the code to check if num is a Numeric type using kind_of?.
num = 42 if num.[1](Numeric) puts "It's a number!" end
The kind_of? method checks if an object is an instance of a class or its subclass. Here, num.kind_of?(Numeric) returns true because 42 is a Numeric.
Fix the error in the code to correctly check if arr is an Array using is_a?.
arr = [1, 2, 3] if arr.[1](Array) puts "It's an array!" end
The method is_a? must include the question mark. Without it, Ruby raises a NoMethodError.
Fill both blanks to create a method that returns true if obj is a String or a subclass of String.
def string_check(obj) obj.[1](String) || obj.[2](String) end
Both is_a? and kind_of? check if an object is an instance of a class or its subclass. Using either returns true for String or its subclasses.
Fill all three blanks to create a hash that maps objects to their type check results using is_a? and kind_of?.
obj1 = [1, 2, 3] obj2 = {a: 1} obj3 = "test" results = { obj1: obj1.[1], obj2: obj2.[2](Hash), obj3: obj3.[3](String) }
Here, obj1.class returns the class of obj1, which is truthy but not a boolean. obj2.kind_of?(Hash) and obj3.is_a?(String) correctly check type including subclasses.