代码之家  ›  专栏  ›  技术社区  ›  Tony

是否可以检查Rails中的文件流是否打开?

  •  1
  • Tony  · 技术社区  · 16 年前

    我有

    f = File.new(contacts_file_path, "wb")
    f.write params[:uploaded_file].read
    f.close
    

    begin
      f = File.new(contacts_file_path, "wb")
      f.write params[:uploaded_file].read
    rescue
      #error
    ensure
      if f.open? then f.close end
    end
    

    但是开着吗?不是函数,我找不到任何api文档。有什么想法吗?

    1 回复  |  直到 16 年前
        1
  •  6
  •   Greg Dan    16 年前
    f.close unless f.closed?