Introduction
Ruby was originally influenced by the scripting language called Perl. Perl stands for Practical Extraction and Reporting Language. Due to this, Ruby has several global functions that aid in writing programs that take out information from files and generate reports.
So, in this article, we will extensively discuss Practical Extraction and Reporting Shortcuts in Ruby in detail.
Practical Extraction and Reporting Shortcuts
As Ruby is a pure Object-Oriented Language, so for the purpose of IO, it has input and output functions along with string manipulation functions for String.
One of the variables used for this purpose is $_.
$_ - This variable stores the last line read from the input stream. The underscore character is just a mnemonic, it appears like a line.
In addition to the global input and output functions, there are many global string processing functions that behave similar to the String methods but implicitly operate on $_. These global variables and functions provide shortcuts for simple and short Ruby scripts.
Note: It is usually a bad idea to rely on them in larger programs.
Let us look at some of them in detail:
Input Functions
The global functions readline, readlines, and gets are a few of the IO methods but they implicitly operate on the $< stream. It is also available as the constant known as ARGF). Similar to the IO methods, these global functions implicitly set $_.
$< acts similar to an IO object. When the ARGV array is empty, then $< acts the same as STDIN, the standard input stream. When ARGV is not empty, then Ruby assumes that it is a list of filenames. In this case, $< acts like it was reading from the concatenation of all those files.
For the first read request of $<, Ruby uses ARGV.shift to discard the first filename from ARGV. It opens and reads from that file. After reaching the end of that file, Ruby repeats the process, shifting the next filename out of ARGV and opening that file. $< reports the end-of-file when there are no more file names in ARGV.
Deprecated Extraction Functions
In 1.8 and before versions of Ruby, the global functions gsub, gsub!, scan, split, chomp, chomp!, chop, chop!, sub, and sub! work like the same-named methods of String, but implicitly operate on $_.
chomp, chop, gsub, and sub assign their result back to $_, which implies that they are effectively synonyms for their exclamation-mark(!) versions.
However, these global functions have been removed in Ruby 1.9, so they should not be used in new code.
Reporting Functions
There are several global functions for sending output to $stdout. print, printf, puts, and putc are the same as the same-named methods of STDOUT.
The global function p is one such with no analog in the IO class. It is meant for debugging, and its short name makes it extremely easy to type. It calls the inspect method of each of its arguments and passes the output strings to puts. Inspect is by default equivalent to to_s, but some classes redefine it to offer more developer-friendly output apt for debugging.
The printf method expects a format string as its first argument and substitutes the value of its remaining arguments into that string before outputting the answer. These work similar to the % operator of String.
One-Line Script Shortcuts
There is one special shortcut inherited from Perl that is allowed only in scripts specified with -e. If a script is mentioned with -e, and a regular expression literal appears by itself in a conditional expression, that is part of an unless, if, while, or until statement or modifier, then the regular expression is compared implicitly to $_.
For example, if all the lines that start with A are to be printed in a file, the script will be:
ruby -n -e 'print if /^A/' datafile
If this same script was stored in a file and run without the -e option, it will still work, but it will show a warning. To prevent the warning, the comparison has to be made explicit:
print if $_ =~ /^A/