StringIO

StringIO

Controls an IO device process that wraps a string.

A StringIO IO device can be passed as a “device” to most of the functions in the IO module.

Examples

iex> {:ok, pid} = StringIO.open("foo")
iex> IO.read(pid, 2)
"fo"

Summary

Functions

close(pid)

Stops the IO device and returns the remaining input/output buffers

contents(pid)

Returns the current input/output buffers for the given IO device

flush(pid)

Flushes the output buffer and returns its current contents

open(string, options \\ [])

Creates an IO device

Functions

close(pid)

close(pid) :: {:ok, {binary, binary}}

Stops the IO device and returns the remaining input/output buffers.

Examples

iex> {:ok, pid} = StringIO.open("in")
iex> IO.write(pid, "out")
iex> StringIO.close(pid)
{:ok, {"in", "out"}}

contents(pid)

contents(pid) :: {binary, binary}

Returns the current input/output buffers for the given IO device.

Examples

iex> {:ok, pid} = StringIO.open("in")
iex> IO.write(pid, "out")
iex> StringIO.contents(pid)
{"in", "out"}

flush(pid)

flush(pid) :: binary

Flushes the output buffer and returns its current contents.

Examples

iex> {:ok, pid} = StringIO.open("in")
iex> IO.write(pid, "out")
iex> StringIO.flush(pid)
"out"
iex> StringIO.contents(pid)
{"in", ""}

open(string, options \\ [])

open(binary, Keyword.t) :: {:ok, pid}

Creates an IO device.

string will be the initial input of the newly created device.

If the :capture_prompt option is set to true, prompts (specified as arguments to IO.get* functions) are captured.

Examples

iex> {:ok, pid} = StringIO.open("foo")
iex> IO.gets(pid, ">")
"foo"
iex> StringIO.contents(pid)
{"", ""}

iex> {:ok, pid} = StringIO.open("foo", capture_prompt: true)
iex> IO.gets(pid, ">")
"foo"
iex> StringIO.contents(pid)
{"", ">"}

© 2012–2017 Plataformatec
Licensed under the Apache License, Version 2.0.
https://hexdocs.pm/elixir/1.4.5/StringIO.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部