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 ...
Get The Ruby Programming Language 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.