ru2 2.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9b2c993f32bd213c93f01412741d905e17055237
4
+ data.tar.gz: b90d80f68ddf4b166fbf8cddcde0df1fa36b4efd
5
+ SHA512:
6
+ metadata.gz: 7121130a3c3bdf3a0a95d2f911ab0b3944f6d014572f77bf587fee9cd79ef387228d848f355ab5de0f041c455196acf40f3cd8770f633f50fee2bfce7bf893ef
7
+ data.tar.gz: e7d9356db4016936d196ca86ea92c205267698b35717fd0f1ae229328c640583f650c26ac96e2c7e508d46597ddd802e82cd7250ea081ae85157cc6767942e68
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ .DS_Store
2
+ Gemfile.lock
3
+ *.gem
4
+ log/*.log
5
+ .bundle/
6
+ pkg/
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ script: bundle exec rspec
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1
7
+ - 2.2
data/Appraisals ADDED
@@ -0,0 +1,11 @@
1
+ appraise "activesupport-3" do
2
+ gem "activesupport", "~>3.0"
3
+ end
4
+
5
+ appraise "activesupport-4" do
6
+ gem "activesupport", "~>4.0"
7
+ end
8
+
9
+ appraise "activesupport-5" do
10
+ gem "activesupport", "~>5.0"
11
+ end
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 Tom Benner
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,324 @@
1
+ Ru
2
+ =====
3
+ Ruby in your shell!
4
+
5
+ <img src="https://raw.github.com/tombenner/ru/master/doc/logo.png" />
6
+
7
+ [<img src="https://secure.travis-ci.org/tombenner/ru.png" />](http://travis-ci.org/tombenner/ru)
8
+
9
+ Overview
10
+ --------
11
+
12
+ Ru brings Ruby's expressiveness, cleanliness, and readability to the command line.
13
+
14
+ It lets you avoid looking up pesky options in man pages and Googling how to write a transformation in bash that would take you approximately 1s to write in Ruby.
15
+
16
+ For example, to center a file's lines, use [String#center](http://ruby-doc.org/core-2.0/String.html#method-i-center):
17
+
18
+ ```bash
19
+ ru 'map(:center, 80)' myfile
20
+ ```
21
+
22
+ Using traditional tools, this isn't as easy or readable:
23
+
24
+ ```bash
25
+ awk 'printf "%" int(40+length($0)/2) "s\n", $0' myfile
26
+ ```
27
+
28
+ For another example, let's compare summing the lines of a list of integers using Ru vs. a traditional approach:
29
+
30
+ ```bash
31
+ ru 'map(:to_i).sum' myfile
32
+ ```
33
+
34
+ ```bash
35
+ awk '{s+=$1} END {print s}' myfile
36
+ ```
37
+
38
+ Any method from Ruby Core and Active Support can be used. Ru also provides new methods (and modifies [#map](#map)) to make transformations easier. Here are some variations on the above example:
39
+
40
+ ```bash
41
+ ru 'map(:to_i, 10).sum' myfile
42
+ ru 'map(:to_i).reduce(&:+)' myfile
43
+ ru 'each_line.to_i.to_a.sum' myfile
44
+ ru 'grep(/^\d+$/).map(:to_i).sum' myfile
45
+ ru 'map { |n| n.to_i }.reduce(&:+)' myfile
46
+ ru 'reduce(0) { |sum, n| sum + n.to_i }' myfile
47
+ ru 'each_line.match(/(\d+)/)[1].to_i.to_a.sum' myfile
48
+ ru 'map { |n| n.to_i }.reduce(0) { |sum, n| sum + n }' myfile
49
+ ```
50
+
51
+ See [Examples](#examples) and [Methods](#methods) for more.
52
+
53
+ Installation
54
+ ------------
55
+
56
+ ```bash
57
+ gem install ru2
58
+ ```
59
+
60
+ You can now use Ruby in your shell!
61
+
62
+ For example, to sum a list of integers:
63
+
64
+ ```bash
65
+ $ printf "2\n3" | ru 'map(:to_i).sum'
66
+ 5
67
+ ```
68
+
69
+ Usage
70
+ -----
71
+
72
+ See [Examples](#examples) below, too!
73
+
74
+ Ru reads from stdin:
75
+
76
+ ```bash
77
+ $ printf "2\n3" | ru 'map(:to_i).sum'
78
+ 5
79
+ $ cat myfile | ru 'map(:to_i).sum'
80
+ 5
81
+ ```
82
+
83
+ Or from file(s):
84
+
85
+ ```bash
86
+ $ ru 'map(:to_i).sum' myfile
87
+ 5
88
+ $ ru 'map(:to_i).sum' myfile myfile
89
+ 10
90
+ ```
91
+
92
+ You can also run Ruby code without any input by prepending a `! `:
93
+
94
+ ```bash
95
+ $ ru '! 2 + 3'
96
+ 5
97
+ ```
98
+
99
+ The code argument is run as if it has `$stdin.each_line.map(&:chomp).` prepended to it. The result is converted to a string and printed. So, if you run `ru 'map(&:to_i).sum'`, you can think of it as running `puts $stdin.each_line.map(&:chomp).map(&:to_i).sum`.
100
+
101
+ In addition to the methods provided by Ruby Core and Active Support, Ru provides other methods for performing transformations, like `each_line`, `files`, and `grep`, and it improves `map`. See [Methods](#methods) for more.
102
+
103
+ Stream mode
104
+ -----------
105
+ If input data is very large or of undefined size it may be better to process it line by line without loading the whole data into memory.
106
+ This can be done by activating stream mode (which utilizes Enumerator::Lazy) by passing `-s` or `--stream` flag.
107
+ Note, that in stream mode Ru can process only one file or input stream at a time.
108
+
109
+ For example, let's count how many lines there are in the /dev/urandom :)
110
+
111
+ ```bash
112
+ $ cat /dev/urandom | ru -s 'inject(0){|a| puts a if a % 100000 == 0; a+1 }'
113
+ ```
114
+
115
+ Or how many zeros there are in the /dev/zero :)
116
+
117
+ Note, that there are no lines in the stream, so we have to active binary mode by passing `-b` or `--binary` flag.
118
+
119
+ ```bash
120
+ $ cat /dev/zero | ru -s -b 'inject(0){|a| puts a if a % 10000000 == 0; a+1 }'
121
+ ```
122
+
123
+ As you can see, this allows to read stream or file byte by byte.
124
+
125
+ ```bash
126
+ $ echo 'test' > /tmp/test && ru -b 'join(" ")' /tmp/test
127
+ 116 101 115 116 10
128
+ ```
129
+
130
+ Note, that memory consumption is constant no matter how long those commands are running.
131
+ You can interrupt them with Ctrl+C.
132
+
133
+ Examples
134
+ --------
135
+
136
+ Let's compare the readability and conciseness of Ru relative to existing tools:
137
+
138
+ #### Center lines
139
+
140
+ ##### ru
141
+ ```bash
142
+ ru 'map(:center, 80)' myfile
143
+ ```
144
+
145
+ ##### awk
146
+ ```bash
147
+ awk 'printf "%" int(40+length($0)/2) "s\n", $0' myfile
148
+ ```
149
+
150
+ ##### sed
151
+ [Script](https://www.gnu.org/software/sed/manual/sed.html#Centering-lines)
152
+
153
+ #### Sum a list of integers
154
+
155
+ ##### ru
156
+ ```bash
157
+ ru 'map(:to_i).sum' myfile
158
+ ```
159
+
160
+ ##### awk
161
+ ```bash
162
+ awk '{s+=$1} END {print s}' myfile
163
+ ```
164
+
165
+ ##### paste
166
+ ```bash
167
+ paste -s -d+ myfile | bc
168
+ ```
169
+
170
+ #### Print the 5th line
171
+
172
+ ##### ru
173
+ ```bash
174
+ ru '[4]' myfile
175
+ ```
176
+
177
+ ##### sed
178
+ ```bash
179
+ sed '5q;d' myfile
180
+ ```
181
+
182
+ #### Print all lines except the first and last
183
+
184
+ ##### ru
185
+ ```bash
186
+ ru '[1..-2]' myfile
187
+ ```
188
+
189
+ ##### sed
190
+ ```bash
191
+ sed '1d;$d' myfile
192
+ ```
193
+
194
+ #### Sort an Apache access log by response time (decreasing, with time prepended)
195
+
196
+ ##### ru
197
+ ```bash
198
+ ru 'map { |line| [line[/(\d+)( ".+"){2}$/, 1].to_i, line] }.sort.reverse.map(:join, " ")' access.log
199
+ ```
200
+
201
+ ##### awk
202
+ ```bash
203
+ awk --re-interval '{ match($0, /(([^[:space:]]+|\[[^\]]+\]|"[^"]+")[[:space:]]+){7}/, m); print m[2], $0 }' access.log | sort -nk 1
204
+ ```
205
+ [Source](https://coderwall.com/p/ueazhw)
206
+
207
+ Methods
208
+ -------
209
+
210
+ In addition to the methods provided by Ruby Core and Active Support, Ru provides other methods for performing transformations.
211
+
212
+ #### each_line
213
+
214
+ Provides a shorthand for calling methods on each iteration of the input. Best explained by example:
215
+
216
+ ```bash
217
+ ru 'each_line.strip.center(80)' myfile
218
+ ```
219
+
220
+ If you'd like to transform it back into a list, call `to_a`:
221
+
222
+ ```bash
223
+ ru 'each_line.strip.to_a.map(:center, 80)' myfile
224
+ ```
225
+
226
+ #### files
227
+
228
+ Converts the lines to `Ru::File` objects (see Ru::File below).
229
+
230
+ ```bash
231
+ $ printf "foo.txt" | ru 'files.map(:updated_at).map(:strftime, ""%Y-%m-%d")'
232
+ 2014-11-08
233
+ ```
234
+
235
+ #### format(format='l')
236
+
237
+ Formats a list of `Ru::File`s. You'll typically call this after calling `files` to transform them into strings:
238
+
239
+ ```bash
240
+ $ ru 'files.format'
241
+ 644 tom staff 3 2014-10-26 09:06 bar.txt
242
+ 644 tom staff 11 2014-11-04 08:29 foo.txt
243
+ ```
244
+
245
+ The default format, `'l'`, is shown above. It prints `[omode, owner, group, size, date, name]`.
246
+
247
+ #### grep
248
+
249
+ Selects lines which match the given regex.
250
+
251
+ ```bash
252
+ $ printf "john\npaul\ngeorge" | ru 'grep(/o[h|r]/)'
253
+ john
254
+ george
255
+ ```
256
+
257
+ #### map
258
+
259
+ This is the same as [Array#map](http://www.ruby-doc.org/core-2.0/Array.html#method-i-map), but it adds a new syntax that allows you to easily pass arguments to a method. For example:
260
+
261
+ ```bash
262
+ $ printf "john\npaul" | ru 'map(:[], 0)'
263
+ j
264
+ p
265
+ $ printf "john\npaul" | ru 'map(:center, 8, ".")'
266
+ ..john..
267
+ ..paul..
268
+ ```
269
+
270
+ Note that the examples above can also be performed with `each_line`:
271
+
272
+ ```bash
273
+ $ printf "john\npaul" | ru 'each_line[0]'
274
+ $ printf "john\npaul" | ru 'each_line.center(8, ".")'
275
+ ```
276
+
277
+ Ru::File
278
+ ------------
279
+
280
+ The [`files`](#files) method returns an enumerable of `Ru::File`s, which are similar to Ruby Core's [`File`](http://ruby-doc.org/core-2.0/File.html). Each one has the following methods:
281
+
282
+ * `basename`
283
+ * `created_at` (alias for ctime)
284
+ * `ctime`
285
+ * `extname`
286
+ * `format` (see the [`format`](#formatformatl) method above)
287
+ * `ftype`
288
+ * `gid`
289
+ * `group`
290
+ * `mode`
291
+ * `mtime`
292
+ * `name` (alias for basename)
293
+ * `omode`
294
+ * `owner`
295
+ * `size`
296
+ * `to_s` (alias for name)
297
+ * `uid`
298
+ * `updated_at` (alias for mtime)
299
+ * `world_readable?`
300
+
301
+ Options
302
+ -------
303
+
304
+ #### -h, --help
305
+
306
+ Print a help page.
307
+
308
+ #### -v, --version
309
+
310
+ Print the installed version of Ru.
311
+
312
+ Testing
313
+ -------
314
+
315
+ Ru is tested against Active Support 3, 4 and 5. If you'd like to submit a PR, please be sure to use [Appraisal](https://github.com/thoughtbot/appraisal) to test your changes in all contexts:
316
+
317
+ ```bash
318
+ appraisal rspec
319
+ ```
320
+
321
+ License
322
+ -------
323
+
324
+ Ru is released under the MIT License. Please see the MIT-LICENSE file for details.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+
5
+ require 'rspec/core/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new :spec do |t|
8
+ t.pattern = 'spec/**/*_spec.rb'
9
+ end
10
+
11
+ task :default => 'spec'
data/bin/ru ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/ru'
4
+
5
+ begin
6
+ process = Ru::Process.new
7
+ result = process.run
8
+ puts result unless result.nil?
9
+ rescue => ex
10
+ $stderr.puts ex.message
11
+ $stderr.puts ex.backtrace.join("\n")
12
+ exit false
13
+ end
data/doc/help.erb ADDED
@@ -0,0 +1,33 @@
1
+ Ru (version <%= version %>)
2
+ Ruby in your shell!
3
+
4
+ Ru brings Ruby's expressiveness, cleanliness, and readability to the command line. It lets you avoid looking up pesky options in man pages and Googling how to write a transformation in bash that would take you approximately 1s to write in Ruby.
5
+
6
+ For example, to center a file's lines, use String#center:
7
+ ru 'map(:center, 80)' myfile
8
+
9
+ To sum the lines of a list of integers:
10
+ ru 'map(:to_i).sum' myfile
11
+
12
+ Ru reads from stdin:
13
+ $ printf "2\n3" | ru 'map(:to_i).sum'
14
+ 5
15
+ $ cat myfile | ru 'map(:to_i).sum'
16
+ 5
17
+
18
+ Or from file(s):
19
+ $ ru 'map(:to_i).sum' myfile
20
+ 5
21
+ $ ru 'map(:to_i).sum' myfile myfile
22
+ 10
23
+
24
+ You can also run Ruby code without any input by prepending a `! `:
25
+ $ ru '! 2 + 3'
26
+ 5
27
+
28
+ The code argument is run as if it has `$stdin.each_line.map(&:chomp).` prepended to it. The result is converted to a string and printed. So, if you run `ru 'map(:to_i).sum'`, you can think of it as `puts $stdin.each_line.map(&:chomp).map(:to_i).sum`.
29
+
30
+ In addition to the methods provided by Ruby Core and Active Support, Ru provides other methods for performing transformations, like `each_line`, `files`, and `grep`.
31
+
32
+ To read more, see the README:
33
+ https://github.com/tombenner/ru
data/doc/logo.png ADDED
Binary file
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activesupport", "~>3.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,46 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ ru2 (2.1.4)
5
+ activesupport (>= 3.2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (3.2.22.5)
11
+ i18n (~> 0.6, >= 0.6.4)
12
+ multi_json (~> 1.0)
13
+ appraisal (1.0.3)
14
+ bundler
15
+ rake
16
+ thor (>= 0.14.0)
17
+ diff-lcs (1.2.5)
18
+ i18n (0.7.0)
19
+ multi_json (1.12.1)
20
+ rake (12.0.0)
21
+ rspec (3.5.0)
22
+ rspec-core (~> 3.5.0)
23
+ rspec-expectations (~> 3.5.0)
24
+ rspec-mocks (~> 3.5.0)
25
+ rspec-core (3.5.4)
26
+ rspec-support (~> 3.5.0)
27
+ rspec-expectations (3.5.0)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.5.0)
30
+ rspec-mocks (3.5.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.5.0)
33
+ rspec-support (3.5.0)
34
+ thor (0.19.4)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ activesupport (~> 3.0)
41
+ appraisal (~> 1.0)
42
+ rspec (~> 3.1)
43
+ ru2!
44
+
45
+ BUNDLED WITH
46
+ 1.13.6
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activesupport", "~>4.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,53 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ ru2 (2.1.4)
5
+ activesupport (>= 3.2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (4.2.7.1)
11
+ i18n (~> 0.7)
12
+ json (~> 1.7, >= 1.7.7)
13
+ minitest (~> 5.1)
14
+ thread_safe (~> 0.3, >= 0.3.4)
15
+ tzinfo (~> 1.1)
16
+ appraisal (1.0.3)
17
+ bundler
18
+ rake
19
+ thor (>= 0.14.0)
20
+ diff-lcs (1.2.5)
21
+ i18n (0.7.0)
22
+ json (1.8.3)
23
+ minitest (5.10.1)
24
+ rake (12.0.0)
25
+ rspec (3.5.0)
26
+ rspec-core (~> 3.5.0)
27
+ rspec-expectations (~> 3.5.0)
28
+ rspec-mocks (~> 3.5.0)
29
+ rspec-core (3.5.4)
30
+ rspec-support (~> 3.5.0)
31
+ rspec-expectations (3.5.0)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.5.0)
34
+ rspec-mocks (3.5.0)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.5.0)
37
+ rspec-support (3.5.0)
38
+ thor (0.19.4)
39
+ thread_safe (0.3.5)
40
+ tzinfo (1.2.2)
41
+ thread_safe (~> 0.1)
42
+
43
+ PLATFORMS
44
+ ruby
45
+
46
+ DEPENDENCIES
47
+ activesupport (~> 4.0)
48
+ appraisal (~> 1.0)
49
+ rspec (~> 3.1)
50
+ ru2!
51
+
52
+ BUNDLED WITH
53
+ 1.13.6
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activesupport", "~>5.0"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,52 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ ru2 (2.1.4)
5
+ activesupport (>= 3.2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (5.0.0.1)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (~> 0.7)
13
+ minitest (~> 5.1)
14
+ tzinfo (~> 1.1)
15
+ appraisal (1.0.3)
16
+ bundler
17
+ rake
18
+ thor (>= 0.14.0)
19
+ concurrent-ruby (1.0.2)
20
+ diff-lcs (1.2.5)
21
+ i18n (0.7.0)
22
+ minitest (5.10.1)
23
+ rake (12.0.0)
24
+ rspec (3.5.0)
25
+ rspec-core (~> 3.5.0)
26
+ rspec-expectations (~> 3.5.0)
27
+ rspec-mocks (~> 3.5.0)
28
+ rspec-core (3.5.4)
29
+ rspec-support (~> 3.5.0)
30
+ rspec-expectations (3.5.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.5.0)
33
+ rspec-mocks (3.5.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.5.0)
36
+ rspec-support (3.5.0)
37
+ thor (0.19.4)
38
+ thread_safe (0.3.5)
39
+ tzinfo (1.2.2)
40
+ thread_safe (~> 0.1)
41
+
42
+ PLATFORMS
43
+ ruby
44
+
45
+ DEPENDENCIES
46
+ activesupport (~> 5.0)
47
+ appraisal (~> 1.0)
48
+ rspec (~> 3.1)
49
+ ru2!
50
+
51
+ BUNDLED WITH
52
+ 1.13.6