Bird
Raised Fist0
Rubyprogramming~10 mins

Is_a? and kind_of? for type checking in Ruby - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the variable obj is a String using is_a?.

Ruby
obj = "hello"
puts obj.[1](String)
Drag options to blanks, or click blank then click option'
Ais_a?
Bkind_of?
Cinstance_of?
Dclass
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'instance_of?' which only checks exact class, not subclasses.
Using 'class' which returns the class object, not a boolean.
2fill in blank
medium

Complete the code to check if num is a Numeric type using kind_of?.

Ruby
num = 42
if num.[1](Numeric)
  puts "It's a number!"
end
Drag options to blanks, or click blank then click option'
Ais_a?
Binstance_of?
Ckind_of?
Drespond_to?
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'instance_of?' which does not consider subclasses.
Using 'respond_to?' which checks for methods, not class type.
3fill in blank
hard

Fix the error in the code to correctly check if arr is an Array using is_a?.

Ruby
arr = [1, 2, 3]
if arr.[1](Array)
  puts "It's an array!"
end
Drag options to blanks, or click blank then click option'
Ais_a
Bis_a?
Ckind_of?
Dkind_of
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the question mark in the method name.
Using 'kind_of' without question mark.
4fill in blank
hard

Fill both blanks to create a method that returns true if obj is a String or a subclass of String.

Ruby
def string_check(obj)
  obj.[1](String) || obj.[2](String)
end
Drag options to blanks, or click blank then click option'
Ais_a?
Binstance_of?
Ckind_of?
Dclass
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'instance_of?' which only checks exact class.
Using 'class' which returns the class object, not a boolean.
5fill in blank
hard

Fill all three blanks to create a hash that maps objects to their type check results using is_a? and kind_of?.

Ruby
obj1 = [1, 2, 3]
obj2 = {a: 1}
obj3 = "test"
results = {
  obj1: obj1.[1],
  obj2: obj2.[2](Hash),
  obj3: obj3.[3](String)
}
Drag options to blanks, or click blank then click option'
Ais_a?
Bkind_of?
Cinstance_of?
Dclass
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'instance_of?' which does not consider subclasses.
Using 'class' expecting a boolean result.

Practice

(1/5)
1. What does the Ruby method is_a? check when called on an object?
easy
A. If the object is equal to a given class
B. If the object responds to a given method
C. If the object is an instance of a given class or its subclass
D. If the object is a string

Solution

  1. Step 1: Understand the purpose of is_a?

    The method is_a? checks if an object is an instance of a specific class or any of its subclasses.
  2. Step 2: Compare with other options

    Options A, C, and D describe different checks unrelated to is_a?.
  3. Final Answer:

    If the object is an instance of a given class or its subclass -> Option C
  4. Quick Check:

    is_a? checks instance or subclass [OK]
Hint: Remember: is_a? checks class or subclass membership [OK]
Common Mistakes:
  • Confusing is_a? with equality check
  • Thinking is_a? checks method presence
  • Assuming is_a? only checks exact class
2. Which of the following is the correct syntax to check if variable obj is a kind of Array in Ruby?
easy
A. obj.is_a(Array)
B. obj.kind_of?(Array)
C. obj.kind_of(Array)
D. obj.is_a?[Array]

Solution

  1. Step 1: Recall method syntax

    Both is_a? and kind_of? require a question mark and parentheses around the class name.
  2. Step 2: Check each option

    obj.kind_of?(Array) uses correct method name and syntax. Options B and C miss the question mark, and D has incorrect bracket usage.
  3. Final Answer:

    obj.kind_of?(Array) -> Option B
  4. Quick Check:

    Correct method syntax includes ? and parentheses [OK]
Hint: Use method? with parentheses: obj.kind_of?(Class) [OK]
Common Mistakes:
  • Omitting the question mark in method name
  • Using square brackets instead of parentheses
  • Confusing is_a and kind_of syntax
3. What will be the output of the following Ruby code?
class Animal; end
class Dog < Animal; end

dog = Dog.new
puts dog.is_a?(Animal)
puts dog.kind_of?(Dog)
puts dog.is_a?(String)
medium
A. true true false
B. false true false
C. true false true
D. false false true

Solution

  1. Step 1: Understand class inheritance

    Dog inherits from Animal, so a Dog object is both a Dog and an Animal instance.
  2. Step 2: Evaluate each method call

    dog.is_a?(Animal) returns true because Dog is subclass of Animal.
    dog.kind_of?(Dog) returns true because dog is instance of Dog.
    dog.is_a?(String) returns false because dog is not a String.
  3. Final Answer:

    true true false -> Option A
  4. Quick Check:

    Inheritance means is_a? true for parent classes [OK]
Hint: Remember inheritance: subclass instances are also parent class instances [OK]
Common Mistakes:
  • Assuming is_a? false for parent classes
  • Mixing up kind_of? and is_a? results
  • Thinking dog is a String
4. Identify the error in this Ruby code snippet:
num = 10
if num.is_a?(Integer)
  puts "Number is integer"
else
  puts "Number is not integer"
end
medium
A. Logic error: should use kind_of? instead of is_a?
B. Syntax error: missing parentheses in is_a
C. Runtime error: is_a? method undefined for Integer
D. No error; code runs and prints 'Number is integer'

Solution

  1. Step 1: Check method usage

    The method is_a? is correctly called with parentheses and a valid class Integer.
  2. Step 2: Verify code logic and syntax

    The if condition is valid and will print "Number is integer" because 10 is an Integer.
  3. Final Answer:

    No error; code runs and prints 'Number is integer' -> Option D
  4. Quick Check:

    Correct syntax and logic means no error [OK]
Hint: Check method spelling and parentheses carefully [OK]
Common Mistakes:
  • Omitting parentheses in method call
  • Confusing is_a? with kind_of? as error
  • Assuming is_a? is undefined for numbers
5. You have a method that accepts any object and should behave differently if the object is a String or a Numeric (including Integer and Float). Which Ruby code correctly uses is_a? or kind_of? to check this?
hard
A. if obj.is_a?(String) puts "It's a string" elsif obj.is_a?(Numeric) puts "It's a number" else puts "Unknown type" end
B. if obj.kind_of?(String) puts "It's a string" elsif obj.kind_of?(Integer) puts "It's a number" else puts "Unknown type" end
C. if obj.is_a?(String) puts "It's a string" elsif obj.kind_of?(Float) puts "It's a number" else puts "Unknown type" end
D. if obj.is_a?(Object) puts "It's a string" elsif obj.is_a?(Numeric) puts "It's a number" else puts "Unknown type" end

Solution

  1. Step 1: Understand class hierarchy

    Numeric is a superclass of Integer and Float, so checking is_a?(Numeric) covers all number types.
  2. Step 2: Analyze each option

    if obj.is_a?(String) puts "It's a string" elsif obj.is_a?(Numeric) puts "It's a number" else puts "Unknown type" end correctly checks for String and then Numeric.
    if obj.kind_of?(String) puts "It's a string" elsif obj.kind_of?(Integer) puts "It's a number" else puts "Unknown type" end checks Integer only, missing Float.
    if obj.is_a?(String) puts "It's a string" elsif obj.kind_of?(Float) puts "It's a number" else puts "Unknown type" end checks only Float, missing Integer.
    if obj.is_a?(Object) puts "It's a string" elsif obj.is_a?(Numeric) puts "It's a number" else puts "Unknown type" end checks Object first, which is always true, so logic fails.
  3. Final Answer:

    Option A code correctly distinguishes String and all Numeric types -> Option A
  4. Quick Check:

    Use Numeric superclass to cover all numbers [OK]
Hint: Check against Numeric to cover all number types [OK]
Common Mistakes:
  • Checking only Integer or Float separately
  • Using Object which is always true
  • Mixing is_a? and kind_of? incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard