StringIO

Introduction #

This module provides an IO device that wraps a string.

Examples

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

Functions #

close(pid)

Specs

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

Stops the IO device and returns remaining buffers.

Examples

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

contents(pid)

Specs

  • contents(pid) :: {binary, binary}

Returns current buffers.

Examples

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

open(string, options \\ [])

Specs

Creates an IO 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)
{"", ">"}