It seems that checking for .eof? is more reliable than the each iterator on Ruby 1.8.7.