Cover by David Flanagan, Yukihiro Matsumoto

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Practical Extraction and Reporting Shortcuts

Ruby was influenced by the scripting language Perl, whose name is an acronym for Practical Extraction and Reporting Language. Because of this, Ruby includes a number of global functions that make it easy to write programs that extract information from files and generate reports. In the object-oriented paradigm, input and output functions are methods of IO, and string manipulation functions are methods of String. For pragmatic reasons, however, it is useful to have global functions that read from and write to predefined input and output streams. In addition to providing these global functions, Ruby follows Perl further and defines special behavior for the functions: many of them operate implicitly on the special method-local variable $_. This variable holds the last line read from the input stream. The underscore character is mnemonic: it looks like a line. (Most of Ruby’s global variables that use punctuation characters are inherited from Perl.) In addition to the global input and output functions, there are several global string processing functions that work like the String methods but operate implicitly on $_.

These global functions and variables are intended as shortcuts for short and simple Ruby scripts. It is generally considered bad form to rely on them in larger programs.

Input Functions

The global functions gets, readline, and readlines are just like the IO methods by the same names (see Reading lines), but they operate implicitly ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required