diff options
author | Burdette Lamar <[email protected]> | 2024-03-06 14:10:24 -0600 |
---|---|---|
committer | GitHub <[email protected]> | 2024-03-06 15:10:24 -0500 |
commit | 16ec54ec4177f959b6912745460fd6a96d906d82 (patch) | |
tree | aeb692786b82d89f8ea78e5d89f9e24addccfff9 /doc/command_line | |
parent | b4a69351ec7d6f0a5e34e3bb586053814be352c0 (diff) |
[DOC] Ruby command-line options (#10138)
Diffstat (limited to 'doc/command_line')
-rw-r--r-- | doc/command_line/field_processing.md | 132 |
1 files changed, 0 insertions, 132 deletions
diff --git a/doc/command_line/field_processing.md b/doc/command_line/field_processing.md deleted file mode 100644 index 4b5a460778..0000000000 --- a/doc/command_line/field_processing.md +++ /dev/null @@ -1,132 +0,0 @@ -## 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." -``` |