Simple Output

At its most basic, standard output is familiar to anyone who’s written a Ruby script before. The simplest hello world script outputs its message to standard output, and when run from the command line we see that message printed to our screens.

Just like standard input, standard output has two references in Ruby: its constant, STDOUT, and the global variable $stdout. Just like with standard input, you should prefer the latter since it respects reassignment.

If you want to output to standard output, it’s generally not necessary to state this explicitly. The two most used methods for outputting text in Ruby, print and puts, both operate on $stdout implicitly, so while we could call:

 
$stdout.puts ​"Hello, world!"

it’s not necessary, ...

Get Text Processing with Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.