Input & Output

OutputPort = dynamic($stdout)

Where to write normal output.

InputPort = dynamic($stdin)

Where to read input from.

ErrorPort = dynamic($stderr)

Where to write error/warning output.

Output

print(x) => x

Write x to-s to ^OutputPort.

Example:

> print(1)
11
> print("hello")
hello"hello"

puts(x) => x

Write x to-s to ^OutputPort, followed by a linebreak.

Example:

> puts(1)
1
1
> puts("hello")
hello
"hello"

with-output-to(filename, mode = "w") &action => any
  | filename is-a(String)?
  | mode is-a(String)?

Set ^OutputPort to write output to filename for the duration of action, ensuring that the file is closed.

Returns the result of action.

Example:

> with-output-to("foo"): puts(42)
42
> with-output-to("foo", "a"): puts("hello")
"hello"
> File open("foo") .read
"42\nhello\n"

with-output-to(io) &action => any

Set ^OutputPort to write to io for the duration of action.

Returns the result of action.

Example:

> require("stringio")
false
> x = StringIO new
#<StringIO:0x549e4>
> with-output-to(x): "hello!" write
NoMethodError: undefined method `write' on an instance of String.
> x string
""

with-error-to(filename, mode = "w") &action => any
  | filename is-a(String)?
  | mode is-a(String)?

Set ^ErrorPort to write error output to filename for the duration of action, ensuring that the file is closed.

Returns the result of action.

Example:

> with-error-to("foo", "a"): warning(.some-warning)
nil
> File open("foo") .read
"42\nhello\ncondition.ay::SimpleWarning: some_warning\n"

with-error-to(io) &action => any

Set ^ErrorPort to write to io for the duration of action.

Returns the result of action.

Example:

> require("stringio")
false
> x = StringIO new
#<StringIO:0x563f0>
> with-error-to(x): warning(.foo)
nil
> x string
"condition.ay::SimpleWarning: foo\n"

Input

read-line(sep = $separator) => String

Read a line of text from ^InputPort, signalling EndOfFile on end of file. Lines are separated by sep. A separator of nil reads the entire contents, and a zero-length separator reads the input one paragraph at a time (separated by two linebreaks).

Example:

> with-input-from("foo"): read-line
"42\n"

read-lines(sep = $separator) => String

Read all lines of text from ^InputPort. Lines are separated by sep. A separator of nil reads the entire contents as one segment, and a zero-length separator reads the input one paragraph at a time (separated by two linebreaks).

Example:

> with-input-from("foo"): read-lines
["42\n", "hello\n", "condition.ay::SimpleWarning: some_warning\n"]

read-byte => Integer

Read a single byte from ^InputPort, signalling EndOfFile on end of file.

Example:

read-char => String

Same as read-byte chr.

Example:

> with-input-from("foo"): read-char
"4"

read(length = nil, buffer = nil) => String or buffer or nil
  | length nil? or length >= 0
  | buffer nil? or buffer is-a(String)?

Read at most length bytes from ^InputPort, or to the end of file if length is nil. If buffer is given, the data read will be written to it.

Example:

> x = ""
""
> with-input-from("foo"): read(10)
"42\nhello\nc"
> with-input-from("foo"): read(10, x)
"42\nhello\nc"
> x
"42\nhello\nc"

with-input-from(filename, mode = "r") &action => any
  | filename is-a(String)?
  | mode is-a(String)?

Set ^InputPort to read input from filename for the duration of action, ensuring that the file is closed.

Returns the result of action.

Example:

> with-input-from("foo"): read-line
"42\n"

with-input-from(io) &action => any

Set ^InputPort to write to io for the duration of action.

Returns the result of action.

Example:

> require("stringio")
false
> x = StringIO new("hello\ngoodbye\n")
#<StringIO:0x5a560>
> with-input-from(x): read-line
"hello\n"