mirror of
https://github.com/ruby/ruby.git
synced 2025-08-25 05:55:46 +02:00
132 lines
3.3 KiB
Markdown
132 lines
3.3 KiB
Markdown
## 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."
|
|
```
|