Detecting Windows Shutdown

I needed to perform an orderly shutdown of a Webrick based Ruby

I thought about trying to catch the WM_QUERYENDSESSION message which is
sent to all programs when a request to shutdown Windows is made, in
order to execute some code before Windows exits.

Does anyone know how to do that in Ruby?

Is it even possible?