【发布时间】:2011-04-10 14:07:19
【问题描述】:
Ruby 的File.open 将模式和选项作为参数。在哪里可以找到完整的模式和选项列表?
【问题讨论】:
-
ruby-doc.org/core-2.0.0/IO.html#method-c-new-label-IO+Open+Mode - 此页面的链接在下面的 Daniels 答案中,但您必须滚动页面才能找到它。这是文档相关部分的直接链接。
Ruby 的File.open 将模式和选项作为参数。在哪里可以找到完整的模式和选项列表?
【问题讨论】:
我猜是Ruby IO module documentation。
Mode | Meaning
-----+--------------------------------------------------------
"r" | Read-only, starts at beginning of file (default mode).
-----+--------------------------------------------------------
"r+" | Read-write, starts at beginning of file.
-----+--------------------------------------------------------
"w" | Write-only, truncates existing file
| to zero length or creates a new file for writing.
-----+--------------------------------------------------------
"w+" | Read-write, truncates existing file to zero length
| or creates a new file for reading and writing.
-----+--------------------------------------------------------
"a" | Write-only, starts at end of file if file exists,
| otherwise creates a new file for writing.
-----+--------------------------------------------------------
"a+" | Read-write, starts at end of file if file exists,
| otherwise creates a new file for reading and
| writing.
-----+--------------------------------------------------------
"b" | Binary file mode (may appear with
| any of the key letters listed above).
| Suppresses EOL <-> CRLF conversion on Windows. And
| sets external encoding to ASCII-8BIT unless explicitly
| specified.
-----+--------------------------------------------------------
"t" | Text file mode (may appear with
| any of the key letters listed above except "b").
【讨论】:
File.open(filename, mode="r" [, opt])。
opt 是 ruby 1.9 的新功能。各种选项记录在 IO.new 中:www.ruby-doc.org/core/IO.html
【讨论】: