Bird
0
0

Which of the following is the correct syntax to flatten a nested array named arr in Ruby?

easy📝 Syntax Q3 of 15
Ruby - Arrays
Which of the following is the correct syntax to flatten a nested array named arr in Ruby?
Aarr->flatten
Bflatten(arr)
Carr.flatten()
Darr.flatten[]
Step-by-Step Solution
Solution:
  1. Step 1: Recall method call syntax in Ruby

    Methods are called on objects using dot notation, like arr.flatten().
  2. Step 2: Check other options for syntax errors

    flatten(arr) is not valid Ruby syntax, arr->flatten is invalid, and arr.flatten[] is incorrect.
  3. Final Answer:

    arr.flatten() -> Option C
  4. Quick Check:

    Correct method call syntax = arr.flatten() [OK]
Quick Trick: Call flatten with dot and parentheses: arr.flatten() [OK]
Common Mistakes:
  • Using function style flatten(arr)
  • Using arrow notation like arr->flatten
  • Adding brackets after flatten without arguments

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes