Ruby - File IO
How can you read the entire content of a file named
config.txt in Ruby without your program crashing if the file does not exist?config.txt in Ruby without your program crashing if the file does not exist?File.read raises an exception if the file is missing.begin-rescue block to catch Errno::ENOENT and handle missing file gracefully.content = File.read('config.txt') || '' does not prevent exception; Use content = File.readlines('config.txt') and check if empty reads lines but does not handle missing file; Use File.open('config.txt') { |f| content = f.read } without rescue lacks error handling.15+ quiz questions · All difficulty levels · Free
Free Signup - Practice All Questions