Bird
0
0

Which of the following is the correct syntax to read all lines from a file named data.txt into an array in Ruby?

easy📝 Syntax Q12 of 15
Ruby - File IO
Which of the following is the correct syntax to read all lines from a file named data.txt into an array in Ruby?
AFile.read_lines[]('data.txt')
BFile.read('data.txt')
CFile.read_lines('data.txt')
DFile.readlines('data.txt')
Step-by-Step Solution
Solution:
  1. Step 1: Recall correct method name

    The correct Ruby method to read lines is File.readlines (no underscore or plural misspelling).
  2. Step 2: Check method call syntax

    The method is called with parentheses and the filename as a string argument.
  3. Final Answer:

    File.readlines('data.txt') -> Option D
  4. Quick Check:

    Correct method name and syntax = File.readlines('data.txt') [OK]
Quick Trick: Use exact method name: File.readlines('filename') [OK]
Common Mistakes:
  • Using underscores instead of camel case
  • Misspelling the method name
  • Omitting parentheses or using wrong brackets

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes