ChatGPT解决这个技术问题 Extra ChatGPT

What are the Ruby File.open modes and options?

Ruby's File.open takes modes and options as arguments. Where do I find a complete list of modes and options?

ruby-doc.org/core-2.0.0/IO.html#method-c-new-label-IO+Open+Mode - The link to this page is in Daniels answer below but you have to scroll the page to get to it. Here's the direct link to the relevant part of the docs.

C
Casimir et Hippolyte

In Ruby IO module documentation, I suppose.

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

Thanks for the list of the moes. But where are the list for the options: File.open(filename, mode="r" [, opt]) => file
Where did you find that? Unfortunately, I can't find File.open(filename, mode="r" [, opt]) in the documentation.
@floatless. in the api for File class. Go to class "File" then click the method "open".
I suppose, it's some experimental, that is not implemented yet. And I still don't get about what API do you speak. Give a link.
If I may add one little thing for "a+", Read starts at beginning of file, not exactly at end of file (in case anyone wondered).
S
Shadwell

opt is new for ruby 1.9. The various options are documented in IO.new : www.ruby-doc.org/core/IO.html


Unfortunately, that link now appears to be stale!