Hint: Use {|var| ...} or do |var| ... end for blocks [OK]
Common Mistakes:
Missing block parameter |file|
Incorrect block delimiters
Omitting do or braces
3. What will be the output of this Ruby code?
File.open('test.txt', 'w') do |f|
f.puts 'Hello'
end
content = File.read('test.txt')
puts content
medium
A. Hello
B. test.txt
C. nil
D. Error: file not found
Solution
Step 1: Write 'Hello' to file inside block
The block writes the string 'Hello' followed by a newline to 'test.txt'.
Step 2: Read and print file content
After the block, the file is closed automatically. Then File.read reads the content, which is 'Hello\n'. Printing it outputs 'Hello'.
Final Answer:
Hello -> Option A
Quick Check:
File.write + read = Hello [OK]
Hint: File.open with block writes and auto-closes file [OK]
Common Mistakes:
Expecting file to be empty
Thinking file is not saved before reading
Confusing file name with content
4. Identify the error in this Ruby code snippet:
file = File.open('log.txt', 'w') do |f|
f.puts 'Log entry'
end
file.puts 'Another entry'
medium
A. file is nil because File.open with block returns the block's value, not the file object.
B. The file is not opened in write mode.
C. Missing block parameter |f| in File.open.
D. File is not closed automatically.
Solution
Step 1: Understand return value of File.open with block
When using a block, File.open returns the block's last expression, not the file object.
Step 2: Analyze variable 'file'
Here, 'file' is assigned the block's return value, which is nil because f.puts returns nil. So calling file.puts causes an error.
Final Answer:
file is nil because File.open with block returns the block's value, not the file object. -> Option A
Quick Check:
File.open block returns block value, not file [OK]
Hint: File.open with block returns block result, not file object [OK]
Common Mistakes:
Expecting File.open with block to return file object
Trying to use file after block ends
Ignoring file mode argument
5. You want to read a file and count how many lines contain the word 'error'. Which Ruby code correctly uses File.open with a block to do this safely?
hard
A. count = 0
File.open('log.txt') { |f| f.each { |line| count += 1 if line.include?('error') } }
puts count
B. count = 0
f = File.open('log.txt', 'r')
f.each_line { |line| count += 1 if line.include?('error') }
f.close
puts count
C. count = 0
File.open('log.txt', 'r') do
each_line { |line| count += 1 if line.include?('error') }
end
puts count
D. count = 0
File.open('log.txt', 'r') do |f|
f.each_line { |line| count += 1 if line.include?('error') }
end
puts count
Solution
Step 1: Use File.open with block and read lines
count = 0
File.open('log.txt', 'r') do |f|
f.each_line { |line| count += 1 if line.include?('error') }
end
puts count opens the file with a block and iterates each line safely.
Step 2: Count lines containing 'error'
Inside the block, it increments count if the line includes 'error'. The file auto-closes after the block.
Final Answer:
count = 0
File.open('log.txt', 'r') do |f|
f.each_line { |line| count += 1 if line.include?('error') }
end
puts count -> Option D
Quick Check:
File.open block + each_line + count = count = 0
File.open('log.txt', 'r') do |f|
f.each_line { |line| count += 1 if line.include?('error') }
end
puts count [OK]
Hint: Use File.open with block and each_line for safe file reading [OK]
Common Mistakes:
Forgetting to close file when not using block
Missing block parameter or using wrong syntax
Using methods outside block scope
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.