January 2024
Intermediate to advanced
718 pages
20h 15m
English
Ruby comes with a comprehensive library to manage input and output (I/O). But, in most of the examples in this book, we’ll stick to a few simple methods. We’ve already come across methods that write output: puts writes its arguments with a newline after each; p also writes its arguments but will produce more debuggable output. Both can be used to write to any I/O object, but, by default, they write to the standard output stream.
You can read input into your program in many ways. Probably the most traditional one is to use the method gets—short for “get string”—which returns the next line from your program’s standard input stream:
| | line = gets |
| | print line |
Because gets returns nil when it reaches the end of input, you can ...
Read now
Unlock full access