Bird
0
0

Which Ruby code snippet correctly reads the entire content of notes.txt into a variable?

easy📝 Conceptual Q2 of 15
Ruby - File IO
Which Ruby code snippet correctly reads the entire content of notes.txt into a variable?
Acontent = File.read('notes.txt')
Bcontent = File.open('notes.txt').each_line
Ccontent = File.readlines('notes.txt').join
Dcontent = File.readlines('notes.txt')
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct method

    File.read reads the entire file content as a string.
  2. Step 2: Analyze other options

    File.open(...).each_line returns an enumerator, not a string; File.readlines returns an array of lines.
  3. Final Answer:

    content = File.read('notes.txt') -> Option A
  4. Quick Check:

    File.read returns full file content as string [OK]
Quick Trick: Use File.read('filename') to get full content string [OK]
Common Mistakes:
  • Using File.readlines which returns array, not string
  • Calling non-existent method File.readlines
  • Using File.open without reading content

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes