Ruby Errno::ENOENT
Open a receipt in a fresh workspace and handle the case where nobody has saved it yet.
A simple example
Errno::ENOENT
- Start with No file named a.txt
- Do this Open a.txt for reading
- Result File missing
Syntax
rescue Errno::ENOENT
Run these examples in Ruby
Save one example at a time as lesson.rb. Use Ruby 2.6 or later.
ruby lesson.rbRecover when a receipt is missing
A clerk requests a receipt before a new folder contains any exports. Handle that missing entry explicitly so the application can give the clerk a useful next action.
require 'tmpdir'
Dir.mktmpdir do |folder|
path = File.join(folder, "today.txt")
begin
puts File.read(path)
rescue Errno::ENOENT
puts "No receipt yet"
end
end
No receipt yet
The example deliberately uses an absent receipt. Permission failures and unrelated errors should still be visible.
Read a saved shift record
The closing clerk saved the file before leaving. Open the expected file and show the desk whether it is available.
Handle an unsubmitted handover
The next shift opens an empty inbox before the first clerk submits a file. Open the expected file and show the desk whether it is available.
What to watch for
An existence check and a later open are separate operations. Handle failure at the point where the file is opened.