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 ab6a5f2

Browse files
updates for version 2.0
1 parent 107282f commit ab6a5f2

35 files changed

+2440
-1134
lines changed

‎LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 Sundeep Agarwal
3+
Copyright (c) 2024 Sundeep Agarwal
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

‎README.md

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
# Ruby one-liners cookbook
1+
# Ruby One-Liners Guide
22

3-
Example based guide for text processing with `ruby` from the command line.
3+
Example based guide for text processing with Ruby from the command line. Visit https://youtu.be/nsWVepZruws for a short video about the book.
44

5-
<p align="center">
6-
<img src="./images/ruby_oneliners.png" width="320px" height="400px" />
7-
</p>
5+
<p align="center"><img src="./images/ruby_oneliners_ls.png" alt="Ruby One-Liners Guide ebook cover image" /></p>
86

9-
The book also includes exercises to test your understanding, which is presented together as a single file in this repo - [Exercises.md](./exercises/Exercises.md)
7+
The book also includes exercises to test your understanding, which are presented together as a single file in this repo [Exercises.md](./exercises/Exercises.md).
108

119
For solutions to the exercises, see [Exercise_solutions.md](./exercises/Exercise_solutions.md).
1210

@@ -16,25 +14,21 @@ See [Version_changes.md](./Version_changes.md) to keep track of changes made to
1614

1715
# E-book
1816

19-
You can purchase the pdf/epub versions of the book using these links:
17+
* You can purchase the pdf/epub versions of the book using these links:
18+
* https://learnbyexample.gumroad.com/l/ruby-oneliners
19+
* https://leanpub.com/ruby-oneliners
20+
* You can also get the book as part of these bundles:
21+
* **Ruby text processing** bundle from https://learnbyexample.gumroad.com/l/ruby-textprocessing or https://leanpub.com/b/ruby-textprocessing
22+
* **Magical one-liners** bundle from https://learnbyexample.gumroad.com/l/oneliners or https://leanpub.com/b/oneliners
23+
* **All books bundle** bundle from https://learnbyexample.gumroad.com/l/all-books
24+
* Includes all my programming books
25+
* See https://learnbyexample.github.io/books/ for a list of other books
2026

21-
* https://learnbyexample.gumroad.com/l/ruby-oneliners
22-
* https://leanpub.com/ruby-oneliners
27+
For a preview of the book, see [sample chapters](./sample_chapters/ruby_oneliners_sample.pdf).
2328

24-
You can also get the book as part of these bundles:
29+
The book can also be [viewed as a single markdown file in this repo](./ruby_oneliners.md). See my blogpost on [generating pdfs from markdown using pandoc](https://learnbyexample.github.io/customizing-pandoc/) if you are interested in the ebook creation process.
2530

26-
* **Ruby text processing** bundle from https://learnbyexample.gumroad.com/l/ruby-textprocessing or https://leanpub.com/b/ruby-textprocessing
27-
* **Magical one-liners** bundle from https://learnbyexample.gumroad.com/l/oneliners or https://leanpub.com/b/oneliners
28-
* **All books bundle** bundle from https://learnbyexample.gumroad.com/l/all-books
29-
* Includes all my programming books
30-
31-
See https://learnbyexample.github.io/books/ for list of other books
32-
33-
For a preview of the book, see [sample chapters](https://github.com/learnbyexample/learn_ruby_oneliners/blob/master/sample_chapters/ruby_oneliners_sample.pdf)
34-
35-
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.
36-
37-
For web version of the book, visit https://learnbyexample.github.io/learn_ruby_oneliners/
31+
For the web version of the book, visit https://learnbyexample.github.io/learn_ruby_oneliners/
3832

3933
<br>
4034

@@ -48,13 +42,15 @@ For web version of the book, visit https://learnbyexample.github.io/learn_ruby_o
4842

4943
# Feedback
5044

51-
Please [open an issue](https://github.com/learnbyexample/learn_ruby_oneliners/issues) if you spot any typo/errors.
52-
5345
⚠️ ⚠️ Please DO NOT submit pull requests. Main reason being any modification requires changes in multiple places.
5446

55-
I'd also highly appreciate your feedback about the book.
47+
I would highly appreciate it if you'd let me know how you felt about this book. It could be anything from a simple thank you, pointing out a typo, mistakes in code snippets, which aspects of the book worked for you (or didn't!) and so on. Reader feedback is essential and especially so for self-published authors.
48+
49+
You can reach me via:
5650

57-
Twitter: https://twitter.com/learn_byexample
51+
* Issue Manager: [https://github.com/learnbyexample/learn_ruby_oneliners/issues](https://github.com/learnbyexample/learn_ruby_oneliners/issues)
52+
* E-mail: `echo 'bGVhcm5ieWV4YW1wbGUubmV0QGdtYWlsLmNvbQo=' | base64 --decode`
53+
* Twitter: [https://twitter.com/learn_byexample](https://twitter.com/learn_byexample)
5854

5955
<br>
6056

@@ -76,24 +72,24 @@ Twitter: https://twitter.com/learn_byexample
7672
# Acknowledgements
7773

7874
* [ruby-lang documentation](https://www.ruby-lang.org/en/documentation/) — manuals and tutorials
79-
* [/r/ruby/](https://www.reddit.com/r/ruby/) — helpful forum for beginners and experienced programmers alike
80-
* [stackoverflow](https://stackoverflow.com/) — for getting answers to pertinent questions on Ruby, one-liners, etc
75+
* [stackoverflow](https://stackoverflow.com/) — for getting answers to pertinent questions on Ruby and related commands
8176
* [tex.stackexchange](https://tex.stackexchange.com/) — for help on [pandoc](https://github.com/jgm/pandoc/) and `tex` related questions
82-
* [LibreOffice Draw](https://www.libreoffice.org/discover/draw/) — cover image
83-
* [pngquant](https://pngquant.org/) and [svgcleaner](https://github.com/RazrFalcon/svgcleaner) for optimizing images
77+
* [/r/ruby/](https://old.reddit.com/r/ruby/) — helpful forum
78+
* [canva](https://www.canva.com/) — cover image
79+
* [oxipng](https://github.com/shssoichiro/oxipng), [pngquant](https://pngquant.org/) and [svgcleaner](https://github.com/RazrFalcon/svgcleaner) — optimizing images
8480
* [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
85-
* [softwareengineering.stackexchange](https://softwareengineering.stackexchange.com/questions/39/whats-your-favourite-quote-about-programming) and [skolakoda](https://skolakoda.org/programming-quotes)for programming quotes
81+
* **KOTP**for spotting grammatical mistakes
8682
* [mdBook](https://github.com/rust-lang/mdBook) — for web version of the book
8783
* [mdBook-pagetoc](https://github.com/JorelAli/mdBook-pagetoc) — for adding table of contents for each chapter
8884
* [minify-html](https://github.com/wilsonzlin/minify-html) — for minifying html files
8985

90-
A heartfelt thanks to all my readers. Your valuable support has significantly eased my financial concerns and allows me to continue writing books.
86+
A heartfelt thanks to all my readers. Your valuable support has significantly eased my financial concerns and allows me to continue working on programming ebooks.
9187

9288
<br>
9389

9490
# License
9591

96-
The book is licensed under a [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-nc-sa/4.0/)
92+
The book is licensed under a [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License](https://creativecommons.org/licenses/by-nc-sa/4.0/).
9793

98-
The code snippets are licensed under MIT, see [LICENSE](./LICENSE) file
94+
The code snippets are licensed under MIT, see [LICENSE](./LICENSE) file.
9995

‎Version_changes.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
<br>
22

3+
### 2.0
4+
5+
* Command version updated to **Ruby 3.3.0**
6+
* Added more exercises
7+
* Long sections split into smaller ones
8+
* In general, many of the examples, exercises, solutions, descriptions and external links were updated/corrected
9+
* Updated Acknowledgements section
10+
* Code snippets related to info/warning sections will now appear as a single block
11+
* Book title changed to **Ruby One-Liners Guide**
12+
* New cover image
13+
14+
<br>
15+
316
### 1.6
417

518
* Corrected usage of substitution methods for fixed string replacement

‎code_snippets/Field_separators.sh

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
echo ' a b c ' | ruby -ane 'puts $F.size'
44

5-
echo ' a b c ' | ruby -ane 'puts $F[0]'
5+
echo ' a b c ' | ruby -ane 'puts "(#{$F[0]})"'
66

7-
echo ' a b c ' | ruby -ane 'puts $F[-1] + "."'
7+
echo ' a b c ' | ruby -ane 'puts "(#{$F[-1]})"'
88

9-
printf ' one \t\f\v two\t\r\tthree ' | ruby -ane 'puts $F.size'
9+
printf ' one \t\f\v two\t\r\tthree \t\r ' | ruby -ane 'puts $F.size'
1010

11-
printf ' one \t\f\v two\t\r\tthree ' | ruby -ane 'puts $F[1] + "."'
11+
printf ' one \t\f\v two\t\r\tthree \t\r ' | ruby -ane 'puts $F[1] + "."'
1212

1313
## Input field separator
1414

15-
echo 'goal:amazing:whistle:kwality' | ruby -F: -ane 'puts $F[0], $F[-1]'
15+
echo 'goal:amazing:whistle:kwality' | ruby -F: -ane 'puts $F[0], $F[-1], $F[1]'
1616

1717
echo 'one;two;three;four' | ruby -F';' -ane 'puts $F[2]'
1818

@@ -22,6 +22,8 @@ echo 'hi.bye.hello' | ruby -F'\.' -ane 'puts $F[1]'
2222

2323
printf 'COOL\nnice car\n' | ruby -F'(?i)[aeiou]' -ane 'puts $F.size - 1'
2424

25+
## Character-wise separation
26+
2527
echo 'apple' | ruby -ne 'puts $_[0]'
2628

2729
ruby -e 'puts Encoding.default_external'
@@ -32,6 +34,8 @@ echo 'fox:αλεπού' | ruby -ne 'puts $_[4..5]'
3234

3335
echo 'fox:αλεπού' | ruby -E UTF-8:UTF-8 -ne 'puts $_[4..5]'
3436

37+
## Newline character in the last field
38+
3539
echo 'cat dog' | ruby -ane 'puts "[#{$F[-1]}]"'
3640

3741
echo 'cat:dog' | ruby -F: -ane 'puts "[#{$F[-1]}]"'
@@ -40,7 +44,9 @@ printf 'cat:dog' | ruby -F: -ane 'puts "[#{$F[-1]}]"'
4044

4145
echo ' a b c ' | ruby -ane 'puts $F.size'
4246

43-
echo ':a:b:c:' | ruby -F: -ane 'puts $F.size'
47+
echo ':a:b:c:' | ruby -F: -ane 'puts $F.size; puts "[#{$F[-1]}]"'
48+
49+
## Using the -l option for field splitting
4450

4551
echo 'cat:dog' | ruby -F: -lane 'puts "[#{$F[-1]}]"'
4652

@@ -68,9 +74,17 @@ echo "$s" | ruby -F: -lane '$F.append(42); puts $F * "::"'
6874

6975
s='Sample123string42with777numbers'
7076

71-
echo "$s" | ruby -lne 'puts $_.scan(/\d+/)[1]'
77+
echo "$s" | ruby -ne 'puts $_.scan(/\d+/)[1]'
78+
79+
s='coat Bin food tar12 best Apple fig_42'
7280

73-
echo "$s" | ruby -lne 'puts $_.scan(/[a-z]+/i) * ","'
81+
echo "$s" | ruby -ne 'puts $_.scan(/\b[a-z0-9]+\b/) * ","'
82+
83+
s='items: "apple" and "mango"'
84+
85+
echo "$s" | ruby -ne 'puts $_.scan(/"[^"]+"/)[1]'
86+
87+
echo "$s" | ruby -ne 'puts $_[/"[^"]+"/]'
7488

7589
s='eagle,"fox,42",bee,frog'
7690

@@ -106,28 +120,30 @@ echo "$s" | ruby -F: -lane 'puts $F.grep(/i[nts]/) * ":"'
106120

107121
echo "$s" | ruby -F: -lane 'puts $F.grep_v(/\d/) * ":"'
108122

123+
ruby -lane 'print if $F.grep(/r/).size <= 1' table.txt
124+
109125
s='goal:amazing:42:whistle:kwality:3.14'
110126

111127
echo "$s" | ruby -F: -lane 'puts $F.map(&:upcase) * ":"'
112128

113-
echo '23 756 -983 5' | ruby -ane 'puts $F.map {|n| n.to_i ** 2} * " "'
129+
echo '23 756 -983 5' | ruby -ane 'puts $F.map {_1.to_i ** 2} * " "'
114130

115131
echo 'AaBbCc' | ruby -lne 'puts $_.chars.map(&:ord) * " "'
116132

117133
echo '3.14,17,6' | ruby -F, -ane 'puts $F.map(&:to_f).sum'
118134

119135
s='hour hand band mat heated pineapple'
120136

121-
echo "$s" | ruby -ane 'puts $F.filter {|w| w[0]!="h" && w.size<6}'
137+
echo "$s" | ruby -ane 'puts $F.filter {_1[0]!="h" && _1.size<6}'
122138

123139
echo "$s" | ruby -ane 'puts $F.filter_map {|w|
124-
w.gsub(/[ae]/, "X") if w[0]=="h"}'
140+
w.gsub(/[ae]/, "X") if w[0]=="h"}'
125141

126142
echo '3.14,17,6' | ruby -F, -lane 'puts $F.map(&:to_f).reduce(100, :+)'
127143

128144
echo '3.14,17,6' | ruby -F, -lane 'puts $F.map(&:to_f).reduce(:*)'
129145

130-
echo '3.14,17,6' | ruby -F, -lane 'puts $F.reduce(1) {|op,n| op*n.to_f}'
146+
echo '3.14,17,6' | ruby -F, -lane 'puts $F.reduce(2) {|op,n| op*n.to_f}'
131147

132148
s='floor bat to dubious four'
133149

@@ -137,20 +153,19 @@ echo "$s" | ruby -ane 'puts $F.sort_by(&:size) * ":"'
137153

138154
echo '23 756 -983 5' | ruby -lane 'puts $F.sort_by(&:to_i) * ":"'
139155

140-
echo 'foobar' | ruby -lne 'puts $_.chars.sort.reverse * ""'
156+
echo 'dragon' | ruby -lne 'puts $_.chars.sort.reverse * ""'
141157

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

144-
echo "$s" | ruby -ane 'puts $F.sort { |a, b|
145-
[b.size, a] <=> [a.size, b] } * ":"'
160+
echo "$s" | ruby -ane 'puts $F.sort_by {|w| [-w.size, w]} * ":"'
146161

147162
s='3,b,a,3,c,d,1,d,c,2,2,2,3,1,b'
148163

149164
echo "$s" | ruby -F, -lane 'puts $F.uniq * ","'
150165

151166
cat marks.txt
152167

153-
ruby -ane 'idx = $F.each_index.sort {|i,j| $F[j] <=> $F[i]} if $.==1;
168+
ruby -ane 'idx = $F.each_index.sort {$F[_2] <=> $F[_1]} if $.==1;
154169
puts $F.values_at(*idx) * "\t"' marks.txt
155170

156171
s='floor bat to dubious four'

0 commit comments

Comments
(0)

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