1

对于目录中的每个文件,我需要对其进行处理,然后将结果写入另一个文件。如果引发超时异常,则继续循环的下一次迭代。

 require 'timeout'
 timeout_in_seconds = 60

 for fl in Dir.glob('/dir/files')
     begin 
     Timeout::timeout(timeout_in_seconds) do 
 #do something here to get $results
 File.open('new_file', 'w') { |file| file.write(results) }
     end
     rescue Timeout::Error
 next
 end

从shell运行错误是:

  syntax error, 'unexpected kRESCUE, expecting kEND
rescue Timeout::Error

关于如何纠正这个问题的任何想法?

4

2 回答 2

1

rescue应该在结束子句之前。

require 'timeout'
timeout_in_seconds = 60

for fl in Dir.glob('/dir/files')
  begin 
    Timeout::timeout(timeout_in_seconds) do 
    #do something here to get $results
    File.open('new_file', 'w') { |file| file.write(results) }
  rescue Timeout::Error

  end
end
于 2013-07-18T18:22:59.833 回答
0

您在上面的代码中错过了两个end。写如下。

require 'timeout'
timeout_in_seconds = 60

 for fl in Dir.glob('/dir/files')
     begin 
     Timeout::timeout(timeout_in_seconds) do 
       #do something here to get $results
       File.open('new_file', 'w') { |file| file.write(results) }
     end
     rescue Timeout::Error
      next # although I am not sure why this is needed.
     end
 end
于 2013-07-18T18:22:48.960 回答