Skip to content

Commit a29179e

Browse files
Move examples to the bottom of docs
1 parent b76cd38 commit a29179e

File tree

1 file changed

+81
-84
lines changed

1 file changed

+81
-84
lines changed

lib/elixir/lib/file.ex

Lines changed: 81 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,6 @@ defmodule File do
256256
Missing parent directories are not created.
257257
Returns `:ok` if successful, or `{:error, reason}` if an error occurs.
258258
259-
## Examples
260-
261-
File.mkdir("test/unit")
262-
#=> :ok
263-
264-
File.mkdir("non/existing")
265-
#=> {:error, :enoent}
266-
267259
Typical error reasons are:
268260
269261
* `:eacces` - missing search or write permissions for the parent
@@ -273,6 +265,14 @@ defmodule File do
273265
* `:enospc` - there is no space left on the device
274266
* `:enotdir` - a component of `path` is not a directory;
275267
on some platforms, `:enoent` is returned instead
268+
269+
## Examples
270+
271+
File.mkdir("test/unit")
272+
#=> :ok
273+
274+
File.mkdir("non/existing")
275+
#=> {:error, :enoent}
276276
"""
277277
@spec mkdir(Path.t()) :: :ok | {:error, posix | :badarg}
278278
def mkdir(path) do
@@ -311,21 +311,21 @@ defmodule File do
311311
Missing parent directories are created. Returns `:ok` if successful, or
312312
`{:error, reason}` if an error occurs.
313313
314-
## Examples
315-
316-
File.mkdir_p("non/existing/parents")
317-
#=> :ok
318-
319-
File.mkdir_p("/usr/sbin/temp")
320-
#=> {:error, :eperm}
321-
322314
Typical error reasons are:
323315
324316
* `:eacces` - missing search or write permissions for the parent
325317
directories of `path`
326318
* `:enospc` - there is no space left on the device
327319
* `:enotdir` - a component of `path` is not a directory
328320
* `:eperm` - missed required permisions
321+
322+
## Examples
323+
324+
File.mkdir_p("non/existing/parents")
325+
#=> :ok
326+
327+
File.mkdir_p("/usr/sbin/temp")
328+
#=> {:error, :eperm}
329329
"""
330330
@spec mkdir_p(Path.t()) :: :ok | {:error, posix | :badarg}
331331
def mkdir_p(path) do
@@ -388,14 +388,6 @@ defmodule File do
388388
Returns `{:ok, binary}`, where `binary` is a binary data object that contains the contents
389389
of `path`, or `{:error, reason}` if an error occurs.
390390
391-
## Examples
392-
393-
File.read("hello.txt")
394-
#=> {:ok, "world"}
395-
396-
File.read("non_existing.txt")
397-
#=> {:error, :enoent}
398-
399391
Typical error reasons:
400392
401393
* `:enoent` - the file does not exist
@@ -407,6 +399,14 @@ defmodule File do
407399
* `:enomem` - there is not enough memory for the contents of the file
408400
409401
You can use `:file.format_error/1` to get a descriptive string of the error.
402+
403+
## Examples
404+
405+
File.read("hello.txt")
406+
#=> {:ok, "world"}
407+
408+
File.read("non_existing.txt")
409+
#=> {:error, :enoent}
410410
"""
411411
@spec read(Path.t()) :: {:ok, binary} | {:error, posix | :badarg | :terminated | :system_limit}
412412
def read(path) do
@@ -424,7 +424,6 @@ defmodule File do
424424
425425
File.read!("non_existing.txt")
426426
** (File.Error) could not read file "non_existing.txt": no such file or directory
427-
428427
"""
429428
@spec read!(Path.t()) :: binary
430429
def read!(path) do
@@ -443,14 +442,6 @@ defmodule File do
443442
`File.Stat` struct. Returns `{:error, reason}` with
444443
the same reasons as `read/1` if a failure occurs.
445444
446-
## Examples
447-
448-
File.stat("hello.txt")
449-
#=> {:ok, %File.Stat{...}}
450-
451-
File.stat("non_existing.txt", time: :posix)
452-
#=> {:error, :enoent}
453-
454445
## Options
455446
456447
The accepted options are:
@@ -466,6 +457,14 @@ defmodule File do
466457
467458
Note: Since file times are stored in POSIX time format on most operating systems,
468459
it is faster to retrieve file information with the `time: :posix` option.
460+
461+
## Examples
462+
463+
File.stat("hello.txt")
464+
#=> {:ok, %File.Stat{...}}
465+
466+
File.stat("non_existing.txt", time: :posix)
467+
#=> {:error, :enoent}
469468
"""
470469
@spec stat(Path.t(), stat_options) :: {:ok, File.Stat.t()} | {:error, posix | :badarg}
471470
def stat(path, opts \\ []) do
@@ -513,14 +512,6 @@ defmodule File do
513512
514513
For more details, see `:file.read_link_info/2`.
515514
516-
## Examples
517-
518-
File.lstat("link_to_hello")
519-
#=> {:ok, %File.Stat{type: :symlink, ...}}
520-
521-
File.lstat("non_existing.txt", time: :posix)
522-
#=> {:error, :enoent}
523-
524515
## Options
525516
526517
The accepted options are:
@@ -535,6 +526,14 @@ defmodule File do
535526
536527
Note: Since file times are stored in POSIX time format on most operating systems,
537528
it is faster to retrieve file information with the `time: :posix` option.
529+
530+
## Examples
531+
532+
File.lstat("link_to_hello")
533+
#=> {:ok, %File.Stat{type: :symlink, ...}}
534+
535+
File.lstat("non_existing.txt", time: :posix)
536+
#=> {:error, :enoent}
538537
"""
539538
@spec lstat(Path.t(), stat_options) :: {:ok, File.Stat.t()} | {:error, posix | :badarg}
540539
def lstat(path, opts \\ []) do
@@ -583,20 +582,19 @@ defmodule File do
583582
584583
For more details, see `:file.read_link/1`.
585584
585+
Typical error reasons are:
586+
587+
* `:einval` - path is not a symbolic link
588+
* `:enoent` - path does not exist
589+
* `:enotsup` - symbolic links are not supported on the current platform
590+
586591
## Examples
587592
588593
File.read_link("link_to_hello")
589594
#=> {:ok, "hello.txt"}
590595
591596
File.read_link("hello.txt")
592597
#=> {:error, :einval}
593-
594-
Typical error reasons are:
595-
596-
* `:einval` - path is not a symbolic link
597-
* `:enoent` - path does not exist
598-
* `:enotsup` - symbolic links are not supported on the current platform
599-
600598
"""
601599
@doc since: "1.5.0"
602600
@spec read_link(Path.t()) :: {:ok, binary} | {:error, posix | :badarg}
@@ -872,16 +870,16 @@ defmodule File do
872870
checks on both source and destination and it also preserves
873871
the file mode after copy.
874872
873+
Typical error reasons are the same as in `open/2`,
874+
`read/1` and `write/3`.
875+
875876
## Examples
876877
877878
File.copy("hello.txt", "hello_copy.txt")
878879
#=> {:ok, 6}
879880
880881
File.copy("non_existing.txt", "copy.txt")
881882
#=> {:error, :enoent}
882-
883-
Typical error reasons are the same as in `open/2`,
884-
`read/1` and `write/3`.
885883
"""
886884
@spec copy(Path.t() | io_device, Path.t() | io_device, pos_integer | :infinity) ::
887885
{:ok, non_neg_integer} | {:error, posix | :badarg | :terminated}
@@ -1000,6 +998,14 @@ defmodule File do
1000998
explicitly disallow copying to a destination which is a directory,
1001999
and an error will be returned if tried.
10021000
1001+
## Options
1002+
1003+
* `:on_conflict` - (since v1.14.0) Invoked when a file already exists in the destination.
1004+
The function receives arguments for `source_file` and `destination_file`. It should
1005+
return `true` if the existing file should be overwritten, `false` if otherwise.
1006+
The default callback returns `true`. On earlier versions, this callback could be
1007+
given as third argument, but such behavior is now deprecated.
1008+
10031009
## Examples
10041010
10051011
File.cp("hello.txt", "hello_copy.txt")
@@ -1012,15 +1018,6 @@ defmodule File do
10121018
10131019
File.cp("non_existing.txt", "copy.txt")
10141020
#=> {:error, :enoent}
1015-
1016-
## Options
1017-
1018-
* `:on_conflict` - (since v1.14.0) Invoked when a file already exists in the destination.
1019-
The function receives arguments for `source_file` and `destination_file`. It should
1020-
return `true` if the existing file should be overwritten, `false` if otherwise.
1021-
The default callback returns `true`. On earlier versions, this callback could be
1022-
given as third argument, but such behavior is now deprecated.
1023-
10241021
"""
10251022
@spec cp(Path.t(), Path.t(), on_conflict: on_conflict_callback) ::
10261023
:ok | {:error, posix | :badarg | :terminated}
@@ -1354,14 +1351,6 @@ defmodule File do
13541351
the functions in `IO` to write to the file will yield much better performance
13551352
than calling this function multiple times.
13561353
1357-
## Examples
1358-
1359-
File.write("hello.txt", "world!")
1360-
#=> :ok
1361-
1362-
File.write("temp", "world!")
1363-
#=> {:error, :eisdir}
1364-
13651354
Typical error reasons are:
13661355
13671356
* `:enoent` - a component of the file name does not exist
@@ -1372,7 +1361,16 @@ defmodule File do
13721361
the parent directories
13731362
* `:eisdir` - the named file is a directory
13741363
1375-
Check `File.open/2` for other available options.
1364+
Check `File.open/2` for the list of available `modes`.
1365+
1366+
## Examples
1367+
1368+
File.write("hello.txt", "world!")
1369+
#=> :ok
1370+
1371+
File.write("temp", "world!")
1372+
#=> {:error, :eisdir}
1373+
13761374
"""
13771375
@spec write(Path.t(), iodata, [mode]) ::
13781376
:ok | {:error, posix | :badarg | :terminated | :system_limit}
@@ -1430,7 +1428,6 @@ defmodule File do
14301428
14311429
File.rm("tmp_dir/")
14321430
#=> {:error, :eperm}
1433-
14341431
"""
14351432
@spec rm(Path.t()) :: :ok | {:error, posix | :badarg}
14361433
def rm(path) do
@@ -1800,14 +1797,14 @@ defmodule File do
18001797
is given. For this reason, we do not recommend passing
18011798
`:delayed_write` to this function.
18021799
1800+
See `open/2` for the list of available `modes`.
1801+
18031802
## Examples
18041803
18051804
File.open("file.txt", [:read, :write], fn file ->
18061805
IO.read(file, :line)
18071806
end)
18081807
#=> {:ok, "file content"}
1809-
1810-
See `open/2` for the list of available `modes`.
18111808
"""
18121809
@spec open(Path.t(), [mode | :ram], (io_device | file_descriptor -> res)) ::
18131810
{:ok, res} | {:error, posix | :badarg | :system_limit}
@@ -1857,14 +1854,14 @@ defmodule File do
18571854
18581855
If it succeeds opening the file, it returns the `function` result on the IO device.
18591856
1857+
See `open/2` for the list of available `modes`.
1858+
18601859
## Examples
18611860
18621861
File.open!("file.txt", [:read, :write], fn file ->
18631862
IO.read(file, :line)
18641863
end)
18651864
#=> "file content"
1866-
1867-
See `open/2` for the list of available `modes`.
18681865
"""
18691866
@spec open!(Path.t(), [mode | :ram], (io_device | file_descriptor -> res)) :: res when res: var
18701867
def open!(path, modes, function) do
@@ -2146,6 +2143,8 @@ defmodule File do
21462143
that is skipped whenever enumerating the stream (if both `:read_offset`
21472144
and `:trim_bom` are given, the offset is skipped after the BOM).
21482145
2146+
See `Stream.run/1` for an example of streaming into a file.
2147+
21492148
## Examples
21502149
21512150
# Read a utf8 text file which may include BOM
@@ -2155,8 +2154,6 @@ defmodule File do
21552154
# Read in 2048 byte chunks rather than lines
21562155
File.stream!("./test/test.data", 2048)
21572156
#=> %File.Stream{path: "./test/test.data", ...}
2158-
2159-
See `Stream.run/1` for an example of streaming into a file.
21602157
"""
21612158
@spec stream!(Path.t(), :line | pos_integer, [stream_mode]) :: File.Stream.t()
21622159
def stream!(path, line_or_bytes, modes)
@@ -2176,14 +2173,6 @@ defmodule File do
21762173
21772174
Returns `:ok` on success, or `{:error, reason}` on failure.
21782175
2179-
## Examples
2180-
2181-
File.chmod("hello.txt", 0o755)
2182-
#=> :ok
2183-
2184-
File.chmod("non_existing.txt", 0o755)
2185-
#=> {:error, :enoent}
2186-
21872176
## Permissions
21882177
21892178
File permissions are specified by adding together the following octal modes:
@@ -2204,6 +2193,14 @@ defmodule File do
22042193
write, read and execute permission to the owner
22052194
and both read and execute permission to group
22062195
and others.
2196+
2197+
## Examples
2198+
2199+
File.chmod("hello.txt", 0o755)
2200+
#=> :ok
2201+
2202+
File.chmod("non_existing.txt", 0o755)
2203+
#=> {:error, :enoent}
22072204
"""
22082205
@spec chmod(Path.t(), non_neg_integer) :: :ok | {:error, posix | :badarg}
22092206
def chmod(path, mode) do

0 commit comments

Comments
 (0)