You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jun 5, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: ruby_one_liners.md
+68-78Lines changed: 68 additions & 78 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -84,7 +84,7 @@ DESCRIPTION
84
84
**Prerequisites and notes**
85
85
86
86
* familiarity with programming concepts like variables, printing, control structures, arrays, etc
87
-
* familiarity with regular expression
87
+
* familiarity with regular expressions
88
88
* this tutorial is primarily focussed on short programs that are easily usable from command line, similar to using `grep`, `sed`, `awk`, `perl` etc
89
89
* unless otherwise specified, consider input as ASCII encoded text only
90
90
* this is an attempt to translate [Perl chapter](./perl_the_swiss_knife.md) to `ruby`, I don't have prior experience of using `ruby`
@@ -197,8 +197,8 @@ I bought two bananas and three mangoes
197
197
198
198
**Further Reading**
199
199
200
-
*[ruby-doc Pre-defined variables](https://ruby-doc.org/core-2.5.0/doc/globals_rdoc.html#label-Pre-defined+variables) for explanation on `$_` and other such special variables
201
-
*[ruby-doc gsub](https://ruby-doc.org/core-2.5.0/String.html#method-i-gsub) for `gsub` syntax details
200
+
*[ruby-doc: Pre-defined variables](https://ruby-doc.org/core-2.5.0/doc/globals_rdoc.html#label-Pre-defined+variables) for explanation on `$_` and other such special variables
201
+
*[ruby-doc: gsub](https://ruby-doc.org/core-2.5.0/String.html#method-i-gsub) for `gsub` syntax details
202
202
203
203
<br>
204
204
@@ -209,9 +209,9 @@ I bought two bananas and three mangoes
209
209
#### <aname="regular-expressions-based-filtering"></a>Regular expressions based filtering
210
210
211
211
* one way is to use `variable =~ /REGEXP/FLAGS` to check for a match
212
-
*`variable !~ /REGEXP/FLAGS` for negated match
212
+
*use `variable !~ /REGEXP/FLAGS` for negated match
213
213
* by default acts on `$_` if variable is not specified
214
-
* see [ruby-doc Regexp](https://ruby-doc.org/core-2.5.0/Regexp.html) for regular expression details
214
+
* see [ruby-doc: Regexp](https://ruby-doc.org/core-2.5.0/Regexp.html) for regular expression details
215
215
* as we need to print only selective lines, use `-n` option
216
216
* by default, contents of `$_` will be printed if no argument is passed to `print`
217
217
@@ -244,7 +244,7 @@ Violets are blue,
244
244
```
245
245
246
246
* using different delimiter
247
-
* quoting from [ruby-doc Percent Strings](https://ruby-doc.org/core-2.5.0/doc/syntax/literals_rdoc.html#label-Percent+Strings)
247
+
* quoting from [ruby-doc: Percent Strings](https://ruby-doc.org/core-2.5.0/doc/syntax/literals_rdoc.html#label-Percent+Strings)
248
248
249
249
> If you are using "(", "[", "{", "<" you must close it with ")", "]", "}", ">" respectively. You may use most other non-alphanumeric characters for percent string delimiters such as "%", "|", "^", etc.
* assuming that you are already familiar with basic [ERE features](./gnu_sed.md#regular-expressions)
926
+
* assuming that you are already familiar with basics of regular expressions
927
+
* if not, see [Ruby Regular Expressions tutorial](https://github.com/learnbyexample/Ruby_Scripting/blob/master/chapters/Regular_expressions.md)
938
928
* examples/descriptions based only on ASCII encoding
939
-
* See [ruby-doc Regexp](https://ruby-doc.org/core-2.5.0/Regexp.html) for syntax and feature details
929
+
* See [ruby-doc: Regexp](https://ruby-doc.org/core-2.5.0/Regexp.html) for syntax and feature details
940
930
* See [rexegg ruby](https://www.rexegg.com/regex-ruby.html) for a bit of ruby regex history and differences with other regex engines
941
931
942
932
<br>
@@ -1021,7 +1011,7 @@ $ # without newline at end of line, both \z and \Z will give same result
1021
1011
```
1022
1012
1023
1013
* delimiters and quoting
1024
-
* from [ruby-doc Percent Strings](https://ruby-doc.org/core-2.5.0/doc/syntax/literals_rdoc.html#label-Percent+Strings)
1014
+
* from [ruby-doc: Percent Strings](https://ruby-doc.org/core-2.5.0/doc/syntax/literals_rdoc.html#label-Percent+Strings)
1025
1015
1026
1016
> If you are using "(", "[", "{", "<" you must close it with ")", "]", "}", ">" respectively. You may use most other non-alphanumeric characters for percent string delimiters such as "%", "|", "^", etc.
The string matched by lookarounds are like word boundaries and anchors, do not constitute as part of matched string. They are termed as **zero-width patterns**
1127
1117
1128
1118
* positive lookbehind `(?<=`
1129
-
* See also [ruby-doc scan](https://ruby-doc.org/core-2.5.0/String.html#method-i-scan)
1119
+
* See also [ruby-doc: scan](https://ruby-doc.org/core-2.5.0/String.html#method-i-scan)
1130
1120
1131
1121
```bash
1132
1122
$ s='foo=5, bar=3; x=83, y=120'
@@ -1330,7 +1320,7 @@ He he he
1330
1320
1331
1321
* block form allows to use `ruby` code for replacement section
1332
1322
1333
-
quoting from [ruby-doc gsub](https://ruby-doc.org/core-2.5.0/String.html#method-i-gsub)
1323
+
quoting from [ruby-doc: gsub](https://ruby-doc.org/core-2.5.0/String.html#method-i-gsub)
1334
1324
1335
1325
>In the block form, the current match string is passed in as a parameter, and variables such as 1ドル, 2ドル, $`, $&, and $' will be set appropriately. The value returned by the block will be substituted for the match on each call.
* See also [ruby-doc Array to Arguments Conversion](https://ruby-doc.org/core-2.5.0/doc/syntax/calling_methods_rdoc.html#label-Array+to+Arguments+Conversion)
2105
+
* See also [ruby-doc: Array to Arguments Conversion](https://ruby-doc.org/core-2.5.0/doc/syntax/calling_methods_rdoc.html#label-Array+to+Arguments+Conversion)
2116
2106
2117
2107
```bash
2118
2108
$ # accessing more than one element in random order
* See also [stackoverflow What does map(&:name) mean in Ruby?](https://stackoverflow.com/questions/1217088/what-does-mapname-mean-in-ruby) for explanation on `&:`
0 commit comments