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


Answers:


377

我想在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 2010年

1
你是在哪里找到那个东西的。不幸的是,我File.open(filename, mode="r" [, opt])在文档中找不到。
Daniel O'Hara 2010年

@floatless。在File类的api中。转到类“文件”,然后单击方法“打开”。
never_had_a_name

1
我想这是实验性的,尚未实现。而且我仍然不知道您在说什么API。给个链接。
Nakilon 2010年

1
如果我可以为“ a +”添加一点内容,则读取从文件的开头开始,而不是从文件的末尾开始(以防有人怀疑)。
yoppuyoppu

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.