Ruby的File.open模式和选项有哪些?

207

Ruby的File.open 接受模式和选项作为参数。在哪里可以找到完整的模式和选项列表?


这个页面的链接在丹尼尔的回答下面,但你需要滚动页面才能找到它。这是文档相关部分的直接链接。 - newUserNameHere
2个回答

422

我想在Ruby IO模块文档中查找。

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").

2
感谢提供最常用的列表。但是选项列表在哪里呢?例如:File.open(filename, mode="r" [, opt]) => file - never_had_a_name
1
你在哪里找到的?不幸的是,我在文档中找不到File.open(filename, mode="r" [, opt]) - Daniel O'Hara
@floatless。在File类的API中。进入“File”类,然后点击“open”方法。 - never_had_a_name
1
我想,这是一些尚未实现的实验性内容。而且我仍然不明白你在说哪个 API。请给一个链接。 - Nakilon
1
如果我可以为"a+"添加一点东西,那就是Read从文件的开头开始,而不是恰好在文件的结尾(以防有人想知道)。 - yoppuyoppu
显示剩余4条评论

6

很遗憾,该链接现在似乎已经失效了! - Carl Smotricz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接