Bird
0
0

Which of the following is the correct syntax to read the entire content of a file named log.txt in Ruby?

easy📝 Syntax Q3 of 15
Ruby - File IO
Which of the following is the correct syntax to read the entire content of a file named log.txt in Ruby?
AFile.read log.txt
BFile.read["log.txt"]
CFile.read('log.txt')
DFile.read{ 'log.txt' }
Step-by-Step Solution
Solution:
  1. Step 1: Recall correct method call syntax

    In Ruby, method arguments must be inside parentheses or passed without ambiguity. File.read('log.txt') is correct.
  2. Step 2: Analyze other options

    File.read log.txt misses quotes around filename, causing error. Options C and D use wrong syntax for method arguments.
  3. Final Answer:

    File.read('log.txt') -> Option C
  4. Quick Check:

    File.read requires filename string in parentheses = B [OK]
Quick Trick: Always put filename string in parentheses for File.read [OK]
Common Mistakes:
  • Omitting quotes around filename
  • Using square brackets instead of parentheses
  • Using curly braces instead of parentheses

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes