Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2cbcd20

Browse files
updates for version 1.5
1 parent e5db9b3 commit 2cbcd20

18 files changed

+786
-147
lines changed

‎README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ For a preview of the book, see [sample chapters](https://github.com/learnbyexamp
2727

2828
The book can also be [viewed as a single markdown file in this repo](./ruby_oneliners.md). See my blogpost on [generating pdf/epub from markdown using pandoc](https://learnbyexample.github.io/customizing-pandoc/) if you are interested in the ebook creation process.
2929

30-
For online `html` version of the book, visit https://learnbyexample.github.io/learn_ruby_oneliners/
30+
For web version of the book, visit https://learnbyexample.github.io/learn_ruby_oneliners/
3131

3232
<br>
3333

@@ -63,8 +63,12 @@ Twitter: https://twitter.com/learn_byexample
6363
* [stackoverflow](https://stackoverflow.com/) — for getting answers to pertinent questions on Ruby, one-liners, etc
6464
* [tex.stackexchange](https://tex.stackexchange.com/) — for help on `pandoc` and `tex` related questions
6565
* [LibreOffice Draw](https://www.libreoffice.org/discover/draw/) — cover image
66+
* [pngquant](https://pngquant.org/) and [svgcleaner](https://github.com/RazrFalcon/svgcleaner) for optimizing images
6667
* [Warning](https://commons.wikimedia.org/wiki/File:Warning_icon.svg) and [Info](https://commons.wikimedia.org/wiki/File:Info_icon_002.svg) icons by [Amada44](https://commons.wikimedia.org/wiki/User:Amada44) under public domain
6768
* [softwareengineering.stackexchange](https://softwareengineering.stackexchange.com/questions/39/whats-your-favourite-quote-about-programming) and [skolakoda](https://skolakoda.org/programming-quotes) for programming quotes
69+
* [mdBook](https://github.com/rust-lang/mdBook) — for web version of the book
70+
* [mdBook-pagetoc](https://github.com/JorelAli/mdBook-pagetoc) — for adding table of contents for each chapter
71+
* [minify-html](https://github.com/wilsonzlin/minify-html) — for minifying html files
6872

6973
A heartfelt thanks to all my readers. Your valuable support has significantly eased my financial concerns and allows me to continue writing books.
7074

‎code_snippets/Field_separators.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ echo "$s" | ruby -ane 'puts $F.sort * ":"'
137137

138138
echo "$s" | ruby -ane 'puts $F.sort_by(&:size) * ":"'
139139

140+
echo '23 756 -983 5' | ruby -lane 'puts $F.sort_by(&:to_i) * ":"'
141+
140142
echo 'foobar' | ruby -lne 'puts $_.chars.sort.reverse * ""'
141143

142144
s='try a bad to good i teal by nice how'

‎code_snippets/Line_processing.sh

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ ruby -ne 'print if $_.match?(/on\b/)' programming_quotes.txt
2828

2929
echo 'Uryyb Jbeyq' | ruby -pe '$_.tr!("a-zA-Z", "n-za-mN-ZA-M")'
3030

31-
printf 'foo:123:baz' | ruby -ne 'puts $_.tr("^0-9", "-")'
31+
echo 'foo:123:baz' | ruby -pe '$_.tr!("^0-9\n", "-")'
3232

33-
printf 'foo:123:baz' | ruby -ne 'puts $_.tr("^0-9", "")'
33+
echo 'foo:123:baz' | ruby -pe '$_.tr!("^0-9\n", "")'
34+
35+
s='orange apple appleseed cab'
36+
37+
echo "$s" | ruby -pe 'gsub(/\b(?!apple\b)\w++/) {$&.tr("a-z", "1-9")}'
3438

3539
## Conditional substitution
3640

@@ -63,13 +67,19 @@ printf 'sea\neat\ndrop\n' | ruby -ne 'print; exit(2) if /at/'
6367

6468
echo $?
6569

66-
## Line numbers
70+
ruby -pe 'exit if /cake/' table.txt
71+
72+
ruby -pe 'exit if /cake/; END{puts "bye"}' table.txt
73+
74+
ruby -pe 'BEGIN{puts "hi"; exit; puts "hello"}; END{puts "bye"}' table.txt
75+
76+
## Line number based processing
6777

6878
ruby -ne 'print if $. == 3' programming_quotes.txt
6979

7080
ruby -ne 'print if $. == 2 || $. == 5' programming_quotes.txt
7181

72-
printf 'gates\nnot\nused\n' | ruby -pe 'gsub(/t/, "*") if $. == 2'
82+
printf 'gates\nnot\nused\n' | ruby -pe '$_.tr!("a-z", "*") if $. == 2'
7383

7484
seq 14 25 | ruby -ne 'print if $. >= 10'
7585

@@ -79,10 +89,6 @@ ruby -ne 'puts "#{$.}:#{$_}" if $<.eof' programming_quotes.txt
7989

8090
ruby -ne 'print if $<.eof' programming_quotes.txt table.txt
8191

82-
seq 14 25 | ruby -ne 'print if 3..5'
83-
84-
seq 14 25 | ruby -ne 'print if (3...5).include?($.)'
85-
8692
seq 3542 4623452 | ruby -ne '(print; exit) if $. == 2452'
8793

8894
seq 3542 4623452 | ruby -ne 'print if $. == 250; (print; exit) if $. == 2452'
@@ -91,18 +97,42 @@ time seq 3542 4623452 | ruby -ne '(print; exit) if $. == 2452' > f1
9197

9298
time seq 3542 4623452 | ruby -ne 'print if $. == 2452' > f2
9399

94-
## Fixed string matching
100+
## Flip-Flop operator
101+
102+
seq 14 25 | ruby -ne 'print if 3..5'
103+
104+
seq 14 25 | ruby -ne 'print if (3...5).include?($.)'
105+
106+
ruby -ne 'print if /are/../by/' programming_quotes.txt
107+
108+
ruby -ne 'print if 5../use/' programming_quotes.txt
109+
110+
ruby -ne 'print if !(/ll/..$<.eof)' programming_quotes.txt table.txt
111+
112+
ruby -ne 'print if 9../worth/' programming_quotes.txt
113+
114+
ruby -ne 'print if /affect/../XYZ/' programming_quotes.txt
115+
116+
## Working with fixed strings
95117

96118
echo 'int a[5]' | ruby -ne 'print if /a[5]/'
97119

98120
echo 'int a[5]' | ruby -ne 'print if $_.include?("a[5]")'
99121

122+
echo 'int a[5]' | ruby -pe 'sub(/a[5]/, "b")'
123+
124+
echo 'int a[5]' | ruby -pe 'sub("a[5]", "b")'
125+
100126
ruby -e 'a=5; puts "value of a:\t#{a}"'
101127

102128
echo 'int #{a}' | ruby -ne 'print if $_.include?(%q/#{a}/)'
103129

130+
echo 'int #{a}' | ruby -pe 'sub(%q/#{a}/, "b")'
131+
104132
echo 'int #{a}' | s='#{a}' ruby -ne 'print if $_.include?(ENV["s"])'
105133

134+
echo 'int #{a\\}' | s='#{a\\}' ruby -pe 'sub(ENV["s"], "b")'
135+
106136
cat eqns.txt
107137

108138
s='a+b' ruby -ne 'print if $_.start_with?(ENV["s"])' eqns.txt
@@ -117,6 +147,22 @@ ruby -ne '$i = $_.index("="); print if $i && $i < 6' eqns.txt
117147

118148
s='a+b' ruby -ne 'print if $_.index(ENV["s"], 1)' eqns.txt
119149

150+
printf 'a.b\na+b\n' | ruby -lne 'print if /^a.b$/'
151+
152+
printf 'a.b\na+b\n' | ruby -lne 'print if $_ == %q/a.b/'
153+
154+
printf '1 a.b\n2 a+b\n' | ruby -lane 'print if $F[1] != %q/a.b/'
155+
156+
echo 'x+y' | ruby -pe 'sub(%q/x+y/, "x\y\0円z")'
157+
158+
echo 'x+y' | r='x\y\0円z' ruby -pe 'sub(%q/x+y/, ENV["r"])'
159+
160+
echo 'x+y' | r='x\y\0円z' ruby -pe 'sub(%q/x+y/, ENV["r"].gsub(/\\/, "\\0円"))'
161+
162+
ruby -e 'puts %q/x\y\0円z/'
163+
164+
echo 'x+y' | ruby -pe 'sub(%q/x+y/, %q/x\y\0円z/.gsub(/\\/, "\\0円"))'
165+
120166
## In-place file editing
121167

122168
cat colors.txt

‎code_snippets/Multiple_file_input.sh

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,39 @@
22

33
ruby -e 'puts ARGV' f[1-3].txt greeting.txt
44

5-
ruby -lne 'print ARGV' f[12].txt table.txt
5+
ruby -ne 'puts "#{ARGV.size}: " + ARGV * ","' f[12].txt table.txt
66

77
ruby -ne 'puts "--- #{ARGF.filename} ---" if $. == 1;
88
print;
99
ARGF.close if ARGF.eof' greeting.txt table.txt
1010

1111
ruby -ne 'print if ARGF.eof' greeting.txt table.txt
1212

13+
ruby -ne '(print; ARGF.close) if $.==2' greeting.txt table.txt
14+
15+
ruby -pe 'ARGF.close if $.>=1' greeting.txt table.txt
16+
1317
ruby -e 'puts gets' greeting.txt
1418

19+
ruby -e 'puts gets, "---", ARGF.read' greeting.txt
20+
1521
ruby -e 'puts readlines' greeting.txt
1622

1723
ruby -e 'puts ARGF.readchar' greeting.txt
1824

1925
## STDIN
2026

21-
echo 'apple' | ruby -e 'puts readline' greeting.txt
27+
printf 'apple\nmango\n' | ruby -e 'puts readline'
28+
29+
printf 'apple\nmango\n' | ruby -e 'puts readline' greeting.txt
2230

23-
echo 'apple' | ruby -e 'puts STDIN.readline' greeting.txt
31+
printf 'apple\nmango\n' | ruby -e 'puts STDIN.readline' greeting.txt
2432

2533
## Skipping remaining contents per file
2634

2735
ruby -ne '(puts ARGF.filename; ARGF.close) if /I/' f[1-3].txt greeting.txt
2836

29-
ruby -ne '$m1=true if /o/; $m2=true if /at/;
37+
ruby -ne '$m1=true if /e\b/; $m2=true if /[bm]at/i;
3038
(puts ARGF.filename; $m1=$m2=false; ARGF.close; next) if $m1 && $m2;
3139
$m1=$m2=false if ARGF.eof' f[1-3].txt greeting.txt
3240

‎code_snippets/Oneliner_introduction.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ r='\Bpar\B'
5858

5959
rgx="$r" ruby -ne 'print if /#{ENV["rgx"]}/' word_anchors.txt
6060

61+
r='\Bpar\B'
62+
63+
ruby -sne 'print if /#{$rgx}/' -- -rgx="$r" word_anchors.txt
64+
6165
## Executing external commands
6266

6367
ruby -e 'system("echo Hello World")'

‎code_snippets/Processing_multiple_records.sh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@ ruby -e 'ip=readlines; n=2; ip.each_with_index { |s, i|
2727

2828
ruby -e 'ip=readlines; n=2; ip.each_with_index { |s, i|
2929
c=i-n; c=0 if c<0;
30-
(puts ip[c..i]; puts "---") if s.match?(/toy|flower/) }' context.txt
30+
(print $s; puts ip[c..i]; $s="---\n") if s.match?(/toy|flower/) }
31+
' context.txt
3132

3233
ruby -e 'ip=readlines; n=2; ip.each_with_index { |s, i| c=i-n;
3334
puts ip[c] if c>=0 && s.match?(/language/) }' context.txt
3435

3536
ruby -ne 'print $p2 if $.>2 && /toy|flower/; $p2=$p1; $p1=$_' context.txt
3637

38+
tac context.txt | ruby -ne 'print if $n.to_i>0 && ($n-=1)==0;
39+
$n=2 if /language/' | tac
40+
3741
## Records bounded by distinct markers
3842

3943
cat uniform.txt
@@ -76,10 +80,9 @@ seq 30 | ruby -ne 'BEGIN{n=1; c=0}; ($f=true; c+=1) if /4/;
7680
seq 30 | ruby -ne 'BEGIN{n=2; c=0}; ($f=true; c+=1) if /4/;
7781
print if $f && c!=n; $f=false if /6/'
7882

79-
seq 30 | ruby -ne '($f=true; buf=$_; $m=false; next) if /4/;
83+
seq 30 | ruby -ne '($f=true; buf=$_; next) if /4/;
8084
buf << $_ if $f;
81-
($f=false; print buf if $m) if /6/;
82-
$m=true if /\A15\Z/'
85+
($f=false; print buf if buf.match?(/^15$/)) if /6/;'
8386

8487
## Broken blocks
8588

‎code_snippets/Record_separators.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ echo "$s" | ruby -l -0072 -ne 'print if /a/'
2222

2323
echo "$s" | ruby -0072 -lne 'print if /a/'
2424

25-
s=' a\t\tb:1000\n\n\n\n123 7777:x y \n \n z '
25+
s=' a\t\tb\n\t\n:1000\n\n\n\n123 7777:x y \n \n z '
2626

2727
printf '%b' "$s" | ruby -0072 -lane 'puts $F * ","'
2828

@@ -50,11 +50,11 @@ s='a\n\n\n\n\n\n\n\n12\n34\n\nhi\nhello\n'
5050

5151
printf '%b' "$s" | ruby -00 -ne 'print if $. <= 2'
5252

53-
s='\n\n\na\nb\n\n12\n34\n\nhi\nhello\n\n\n\n'
53+
s='\n\n\na\n\n12\n34\n\nhi\nhello\n\n\n\n'
5454

55-
printf '%b' "$s" | ruby -00 -lne 'puts "#{$_}\n---\n" if $. <= 2'
55+
printf '%b' "$s" | ruby -00 -lne 'puts "#{$_}\n---" if $. <= 2'
5656

57-
printf '%b' "$s" | ruby -00 -lne 'puts "#{$_}\n---\n" if $<.eof'
57+
printf '%b' "$s" | ruby -00 -lne 'puts "#{$_}\n---" if $<.eof'
5858

5959
printf '%b' "$s" | ruby -00 -ne 'END{puts $.}'
6060

‎code_snippets/Two_file_processing.sh

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,27 @@ ruby -lane 'BEGIN{g = %w[D C B A S]};
4646

4747
## gets
4848

49-
ruby -pe 'BEGIN{m=3; n=2; n.times {$s = STDIN.gets}}
49+
ruby -pe 'BEGIN{m=3; n=2; n.times {$s = STDIN.gets}};
5050
$_ = $s if $. == m' <greeting.txt table.txt
5151

5252
ruby -ne 'a=$_; n = STDIN.gets.split[-1].to_f;
5353
print a if n > 0' <table.txt greeting.txt
5454

55+
## Multiline fixed string substitution
56+
57+
head -n2 table.txt > search.txt
58+
59+
cat repl.txt
60+
61+
ruby -0777 -ne 'ARGV.size==2 ? s=$_ : ARGV.size==1 ? r=$_ :
62+
print(gsub(s, r.gsub(/\\/, "\\0円")))
63+
' search.txt repl.txt table.txt
64+
65+
## Add file content conditionally
66+
67+
ruby -pe 'BEGIN{r = STDIN.read}; $_ = r if /[ot]/' <dept.txt greeting.txt
68+
69+
ruby -pe 'BEGIN{r = STDIN.read}; print r if /nice/' <dept.txt greeting.txt
70+
71+
ruby -pe 'BEGIN{r = STDIN.read}; $_ << r if /nice/' <dept.txt greeting.txt
72+

‎example_files/repl.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
21ドル$&30円x\\yz
2+
wise ice go goa

‎example_files/search.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
brown bread mat hair 42
2+
blue cake mug shirt -7

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /