簡體   English   中英

File.open沒有正確記錄或我錯過了什么?

[英]File.open not properly documented or am I missing something?

我不確定Ruby的File.open方法是否沒有正確記錄,或者我錯過了什么。

File類繼承自IO ,這似乎是open方法的定義。 據我從文檔中可以看出, File似乎沒有覆蓋IOopen方法的實現。

File文檔將IO.open類方法記錄為采用數字文件描述符參數,可能是IO.sysopen返回的對象。 但是,顯然沒有文檔的File.open方法只需要一個文件名。

例如,根據文檔,這會失敗並且非常正確:

IO.open('data/actors.list') do |io| 
    #...
end

另一方面,這工作:

File.open('data/actors.list') do |io| 
    #...
end

問題是, File.open似乎覆蓋IO.open並具有不同的接口,但它沒有記錄 - 或者至少沒有出現。

我錯過了什么嗎? 這里發生了什么?

我沒有深入挖掘為什么 Ruby-Doc.Org以這種方式顯示它(我還沒有使用Ruby-Doc.Org),但這就是RubyDoc.Info所說的:

方法: File.open

定義於: io.c

  • (File) open(filename, mode = "r"[, opt])
  • (File) open(filename[, mode [, perm]][, opt])
  • (Object) open(filename, mode = "r"[, opt]) {|file| … }
  • (Object) open(filename[, mode [, perm]][, opt]) {|file| … }

沒有關聯的塊, openFile.new的同義詞。 如果給出了可選的代碼塊,它將作為參數傳遞給文件,當塊終止時, File對象將自動關閉。 在這種情況下, File.open返回塊的值。

重載:

  • (File) open(filename, mode = "r"[, opt])
    • 返回:( (File)
  • (File) open(filename[, mode [, perm]][, opt])
    • 返回:( (File)
  • (Object) open(filename, mode = "r"[, opt]) {|file| … }
    • 收益率:( (file)
    • 返回:( (Object)
  • (Object) open(filename[, mode [, perm]][, opt]) {|file| … }
    • 收益率:( (file)
    • 返回:( (Object)

這匹配YARV源文件io.c的RDoc注釋:

/*
 *  Document-method: File::open
 *
 *  call-seq:
 *     File.open(filename, mode="r" [, opt])                 -> file
 *     File.open(filename [, mode [, perm]] [, opt])         -> file
 *     File.open(filename, mode="r" [, opt]) {|file| block } -> obj
 *     File.open(filename [, mode [, perm]] [, opt]) {|file| block } -> obj
 *
 *  With no associated block, <code>open</code> is a synonym for
 *  <code>File.new</code>. If the optional code block is given, it will
 *  be passed <i>file</i> as an argument, and the File object will
 *  automatically be closed when the block terminates. In this instance,
 *  <code>File.open</code> returns the value of the block.
 */

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM