Ruby's File.open
takes modes and options as arguments. Where do I find a complete list of modes and options?
Asked
Active
Viewed 1.3e+01k times
205

Anthony Mastrean
- 21,850
- 21
- 110
- 188

never_had_a_name
- 90,630
- 105
- 267
- 383
-
http://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. – newUserNameHere Oct 30 '13 at 22:28
2 Answers
419
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").

Casimir et Hippolyte
- 88,009
- 5
- 94
- 125

Daniel O'Hara
- 13,307
- 3
- 46
- 68
-
2Thanks for the list of the moes. But where are the list for the options: File.open(filename, mode="r" [, opt]) => file – never_had_a_name Sep 10 '10 at 05:23
-
1Where did you find that? Unfortunately, I can't find `File.open(filename, mode="r" [, opt])` in the documentation. – Daniel O'Hara Sep 10 '10 at 05:29
-
@floatless. in the api for File class. Go to class "File" then click the method "open". – never_had_a_name Sep 10 '10 at 05:31
-
1I 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. – Nakilon Sep 10 '10 at 05:31
-
I can see only `File.new(filename [, mode [, perm]]) => file` and the next comment: `Optional permission bits may be given in perm.`, so `File.new("newfile", File::CREAT|File::TRUNC|File::RDWR, 0644)`. Huh? (Link: http://ruby-doc.org/core/classes/File.src/M002579.html) – Daniel O'Hara Sep 10 '10 at 05:35
-
It's not `DOS/Windows` only, at least not anymore - cfr http://ruby-doc.org/core-1.9.3/IO.html – asymmetric Jan 11 '13 at 17:55
-
1If I may add one little thing for "a+", Read starts at beginning of file, not exactly at end of file (in case anyone wondered). – yoppuyoppu Sep 16 '15 at 13:14
-
-
@DanielO'Hara want to check this `http://stackoverflow.com/questions/42757267/why-does-a-mode-in-ruby-and-python-append-mode-start-from-the-begin-of-file` – Ratatouille Mar 13 '17 at 05:46
6
opt
is new for ruby 1.9. The various options are documented in IO.new
: www.ruby-doc.org/core/IO.html

Shadwell
- 34,314
- 14
- 94
- 99