At exit method
puts 'script start'
at_exit do
puts 'inside at_exit method for the first time'
end
at_exit do
puts 'inside at_exit method for the second time'
end
puts "script end"
at_exit do
if $!
puts 'Exiting'
end
end
(Thread.current[:errors] ||= []) << 'Any error message goes here'
def log_error(error_message)
(Thread.current[:errors] ||= []) << "#{error_message}"
end
at_exit do
File.open('errors.txt', 'a') do |file|
(Thread.current[:errors] ||= []).each do |error|
file.puts error
end
end
end
View Source