Skip to content

Commit 8ec273b

Browse files
authored
Merge pull request #24996 from JuliaLang/ksh/readingrainbow
Doctests for reading functions
2 parents ff045af + edecee7 commit 8ec273b

File tree

1 file changed

+55
-3
lines changed

1 file changed

+55
-3
lines changed

base/io.jl

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,27 @@ read!(filename::AbstractString, a) = open(io->read!(io, a), filename)
288288
289289
Read a string from an I/O stream or a file, up to and including the given delimiter byte.
290290
The text is assumed to be encoded in UTF-8.
291+
292+
# Examples
293+
```jldoctest
294+
julia> open("my_file.txt", "w") do io
295+
write(io, "JuliaLang is a GitHub organization.\nIt has many members.\n");
296+
end
297+
57
298+
299+
julia> readuntil("my_file.txt", 'L')
300+
"JuliaL"
301+
302+
julia> readuntil("my_file.txt", '.')
303+
"JuliaLang is a GitHub organization."
304+
305+
julia> rm("my_file.txt")
306+
```
291307
"""
292308
readuntil(filename::AbstractString, args...) = open(io->readuntil(io, args...), filename)
293309

294310
"""
295-
readline(stream::IO=STDIN; chomp::Bool=true)
311+
readline(io::IO=STDIN; chomp::Bool=true)
296312
readline(filename::AbstractString; chomp::Bool=true)
297313
298314
Read a single line of text from the given I/O stream or file (defaults to `STDIN`).
@@ -301,6 +317,22 @@ input end with `'\\n'` or `"\\r\\n"` or the end of an input stream. When `chomp`
301317
true (as it is by default), these trailing newline characters are removed from the
302318
line before it is returned. When `chomp` is false, they are returned as part of the
303319
line.
320+
321+
# Examples
322+
```jldoctest
323+
julia> open("my_file.txt", "w") do io
324+
write(io, "JuliaLang is a GitHub organization.\nIt has many members.\n");
325+
end
326+
57
327+
328+
julia> readline("my_file.txt")
329+
"JuliaLang is a GitHub organization."
330+
331+
julia> readline("my_file.txt", chomp=false)
332+
"JuliaLang is a GitHub organization.\n"
333+
334+
julia> rm("my_file.txt")
335+
```
304336
"""
305337
function readline(filename::AbstractString; chomp::Bool=true)
306338
open(filename) do f
@@ -321,12 +353,32 @@ function readline(s::IO=STDIN; chomp::Bool=true)
321353
end
322354

323355
"""
324-
readlines(stream::IO=STDIN; chomp::Bool=true)
356+
readlines(io::IO=STDIN; chomp::Bool=true)
325357
readlines(filename::AbstractString; chomp::Bool=true)
326358
327359
Read all lines of an I/O stream or a file as a vector of strings. Behavior is
328-
equivalent to saving the result of reading `readline` repeatedly with the same
360+
equivalent to saving the result of reading [`readline`](@ref) repeatedly with the same
329361
arguments and saving the resulting lines as a vector of strings.
362+
363+
# Examples
364+
```jldoctest
365+
julia> open("my_file.txt", "w") do io
366+
write(io, "JuliaLang is a GitHub organization.\nIt has many members.\n");
367+
end
368+
57
369+
370+
julia> readlines("my_file.txt")
371+
2-element Array{String,1}:
372+
"JuliaLang is a GitHub organization."
373+
"It has many members."
374+
375+
julia> readlines("my_file.txt", chomp=false)
376+
2-element Array{String,1}:
377+
"JuliaLang is a GitHub organization.\n"
378+
"It has many members.\n"
379+
380+
julia> rm("my_file.txt")
381+
```
330382
"""
331383
function readlines(filename::AbstractString; chomp::Bool=true)
332384
open(filename) do f

0 commit comments

Comments
 (0)