Bird
0
0

How can you use Ruby's object model to filter an array to only objects of class String?

hard📝 Application Q9 of 15
Ruby - Basics and Runtime
How can you use Ruby's object model to filter an array to only objects of class String?
AUse array.filter_class(String)
BUse array.select { |obj| obj.is_a?(String) }
CUse array.only_strings
DUse array.find_all_strings
Step-by-Step Solution
Solution:
  1. Step 1: Understand filtering by class

    We want to keep only elements that are strings.
  2. Step 2: Use is_a? method with select

    Using select with is_a?(String) keeps only string objects.
  3. Final Answer:

    Use array.select { |obj| obj.is_a?(String) } -> Option B
  4. Quick Check:

    Filter with select and is_a? for class check [OK]
Quick Trick: Use select with is_a? to filter by class [OK]
Common Mistakes:
  • Using non-existent methods like filter_class
  • Trying to call only_strings on arrays
  • Confusing find_all_strings with Ruby methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes