【问题标题】:What are the Ruby File.open modes and options?Ruby File.open 模式和选项是什么?
【发布时间】:2011-04-10 14:07:19
【问题描述】:

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

【问题讨论】:

标签: ruby file-io


【解决方案1】:

我猜是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").

【讨论】:

  • 感谢moes的列表。但是选项列表在哪里: File.open(filename, mode="r" [, opt]) => file
  • 你在哪里找到的?不幸的是,我在文档中找不到File.open(filename, mode="r" [, opt])
  • @floatless。在 File 类的 api 中。转到“文件”类,然后单击“打开”方法。
  • 我想,这是一些实验性的,尚未实现。而且我仍然不明白你说的是什么 API。给个链接。
  • 如果我可以为“a+”添加一点点,读取从文件开头开始,而不是在文件末尾(以防有人想知道)。
【解决方案2】:

opt 是 ruby​​ 1.9 的新功能。各种选项记录在 IO.new 中:www.ruby-doc.org/core/IO.html

【讨论】:

  • 不幸的是,该链接现在似乎已过时!
猜你喜欢
  • 1970-01-01
  • 2013-01-27
  • 1970-01-01
  • 1970-01-01
  • 2011-06-15
  • 2012-09-24
  • 1970-01-01
  • 2010-09-23
  • 1970-01-01
相关资源
最近更新 更多