Recall & Review
beginner
What does
File.read do in Ruby?File.read reads the entire content of a file and returns it as a string.
Click to reveal answer
beginner
How do you read the whole content of a file named
example.txt in Ruby?You use File.read("example.txt") to get the full content as a string.
Click to reveal answer
beginner
What type of value does
File.read return?It returns a string containing all the text inside the file.
Click to reveal answer
intermediate
What happens if the file does not exist when you use
File.read?Ruby raises an Errno::ENOENT error because it cannot find the file.
Click to reveal answer
intermediate
Can
File.read read binary files as well as text files?Yes, File.read can read any file type, returning the raw bytes as a string.
Click to reveal answer
What does
File.read('data.txt') return?✗ Incorrect
File.read returns the whole file content as a string, not lines or file size.
What error occurs if
File.read tries to open a missing file?✗ Incorrect
Ruby raises Errno::ENOENT when the file is not found.
Which of these is a correct way to read a file's entire content in Ruby?
✗ Incorrect
File.read reads the whole file content as a string.
If you want to read a file's content line by line, which method is better than
File.read?✗ Incorrect
File.readlines returns an array of lines, useful for line-by-line processing.
Can
File.read be used to read binary files like images?✗ Incorrect
File.read reads any file type, returning raw bytes as a string.
Explain how
File.read works in Ruby and what it returns.Think about what you get when you open a book and read all pages at once.
You got /3 concepts.
Describe what happens if you try to use
File.read on a file that does not exist.Imagine trying to open a door that is not there.
You got /3 concepts.