[DOC] Doc for field processing (#10008)

This commit is contained in:
Burdette Lamar 2024-02-21 14:42:23 -06:00 committed by GitHub
parent 577d07cfc6
commit fce780473c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 133 additions and 0 deletions

View File

@ -9,3 +9,4 @@ rdoc
regexp
rjit
yjit
command_line

View File

@ -0,0 +1,132 @@
## Field Processing
Ruby supports <i>field processing</i>.
This means that when certain command-line options are given,
the invoked Ruby program can process input line-by-line.
### About the Examples
Examples here assume that file `desiderata.txt` exists:
```
$ cat desiderata.txt
Go placidly amid the noise and the haste,
and remember what peace there may be in silence.
As far as possible, without surrender,
be on good terms with all persons.
```
The examples also use command-line option `-e`,
which passes the Ruby code to be executed on the command line itself:
```sh
$ ruby -e 'puts "Hello, World."'
```
### Option `-n`
Option `-n` runs your program in a Kernel#gets loop:
```
while gets
# Your Ruby code.
end
```
Note that `gets` reads the next line and sets global variable `$_`
to the last read line:
```sh
$ ruby -n -e 'puts $_' desiderata.txt
Go placidly amid the noise and the haste,
and remember what peace there may be in silence.
As far as possible, without surrender,
be on good terms with all persons.
```
### Option `-p`
Option `-p` is like option `-n`, but also prints each line:
```sh
$ ruby -p -e 'puts $_.size' desiderata.txt
42
Go placidly amid the noise and the haste,
49
and remember what peace there may be in silence.
39
As far as possible, without surrender,
35
be on good terms with all persons.
```
### Option `-a`
Option `-a`, when given with either of options `-n` or `-p`,
splits the string at `$_` into an array of strings at `$F`:
```sh
$ ruby -an -e 'p $F' desiderata.txt
["Go", "placidly", "amid", "the", "noise", "and", "the", "haste,"]
["and", "remember", "what", "peace", "there", "may", "be", "in", "silence."]
["As", "far", "as", "possible,", "without", "surrender,"]
["be", "on", "good", "terms", "with", "all", "persons."]
```
For the splitting,
the default record separator is `$/`,
and the default field separator is `$;`.
### Option `-F`
Option `-F`, when given with option `-a`,
specifies that its argument is to be the input field separator to be used for splitting:
```sh
$ ruby -an -Fs -e 'p $F' desiderata.txt
["Go placidly amid the noi", "e and the ha", "te,\n"]
["and remember what peace there may be in ", "ilence.\n"]
["A", " far a", " po", "", "ible, without ", "urrender,\n"]
["be on good term", " with all per", "on", ".\n"]
```
The argument may be a regular expression:
```
$ ruby -an -F'[.,]\s*' -e 'p $F' desiderata.txt
["Go placidly amid the noise and the haste"]
["and remember what peace there may be in silence"]
["As far as possible", "without surrender"]
["be on good terms with all persons"]
```
### Option `-l`
Option `-l`, when given with option `-n` or `-p`,
modifies line-ending processing by:
- Setting global variable output record separator `$\`
input record separator `$/`;
this affects line-oriented output (such a that from Kernel#puts).
- Calling String#chop! on each line read.
Without option `-l` (unchopped):
```sh
$ ruby -n -e 'p $_' desiderata.txt
"Go placidly amid the noise and the haste,\n"
"and remember what peace there may be in silence.\n"
"As far as possible, without surrender,\n"
"be on good terms with all persons.\n"
```
With option `-l' (chopped):
```sh
$ ruby -ln -e 'p $_' desiderata.txt
"Go placidly amid the noise and the haste,"
"and remember what peace there may be in silence."
"As far as possible, without surrender,"
"be on good terms with all persons."
```