[英]File.open not properly documented or am I missing something?
我不確定Ruby的File.open
方法是否沒有正確記錄,或者我錯過了什么。
File
類繼承自IO
,這似乎是open
方法的定義。 據我從文檔中可以看出, File
似乎沒有覆蓋IO
的open
方法的實現。
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| … }
沒有關聯的塊,
open
是File.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)
/*
* 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.
*/
ruby文檔是錯誤的,File.open會覆蓋IO.open以接受文件名:
http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_file.html#File.new
和
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.