1
0
forked from Mirror/wren

Tweak IO library a bit:

- Reword some docs.
- Make IO.printAll() public.
- Require () on read.
This commit is contained in:
Bob Nystrom
2015-07-18 14:09:00 -07:00
parent ea2bad3272
commit d91c06d18b
5 changed files with 96 additions and 80 deletions

View File

@ -7,38 +7,54 @@ The IO class can be used to read and write to and from the console.
### IO.**print**(objects...)
Prints any number of things to the console and then prints a newline
character. If you don't pass it a string, it will be converted to a string for
you. When passed multiple things, Wren outputs one after another.
Prints a series of objects to the console followed by a newline. Each object is
converted to a string by calling `toString` on it. This is overloaded to
support up to 16 objects. To pass more, use `printAll()`.
> IO.print("I like bananas")
I like bananas
> IO.print("Oranges", 10)
Oranges10
> IO.print("I like bananas")
I like bananas
> IO.print("Oranges", 10)
Oranges10
>
### IO.**printAll**(sequence)
Iterates over [sequence] and prints each element, then prints a single newline
at the end. Each element is converted to a string by calling `toString` on it.
> IO.printAll([1, [2, 3], 4])
1[2, 3]4
### IO.**write**(object)
Prints a single thing to the console, but does not print a newline character
afterwards. If you pass it something that isn't a string, it will convert it to
a string.
Prints a single value to the console, but does not print a newline character
afterwards. Converts the value to a string by calling `toString` on it.
> IO.write(4 + 5)
9>
> IO.write(4 + 5)
9>
In the above example, the result of `4 + 5` is printed, and then the prompt is
printed on the same line because no newline character was printed afterwards.
### IO.**read**()
Reads in a line of text from stdin. Note that the returned text includes the
trailing newline.
> var name = IO.read()
John
> IO.print("Hello " + name + "!")
Hello John
!
>
### IO.**read**(prompt)
Reads in and returns a line of text from the console. Takes a single string to
be used as a prompt. Pass an empty string for no prompt. Note that the returned
line of text includes the newline character at the end.
Displays `prompt` then reads in a line of text from stdin. Note that the
returned text includes the trailing newline.
> var name = IO.read("Enter your name: ")
Enter your name: John
> IO.print("Hello " + name + "!")
Hello John
!
>
> var name = IO.read("Enter your name: ")
Enter your name: John
> IO.print("Hello " + name + "!")
Hello John
!
>