Consider implementing this as a finalizer:
http://www.mikeperham.com/2010/02/24/the-trouble-with-ruby-finalizers/
http://www.ruby-doc.org/core-1.9.3/ObjectSpace.html