Rushed
Ruby in your shell!
Overview
Rushed 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.
For example, to center a file’s lines, use String#center:
“sh ru ‘map(:center, 80)’ myfile
“
Using traditional tools, this isn’t as easy or readable:
“sh awk ‘printf “%” int(40+length($0)/2) “s\n”, $0’ myfile
“
For another example, let’s compare summing the lines of a list of integers using Rushed vs. a traditional approach:
“sh ru ‘map(:to_i).sum’ myfile
“
“sh awk ‘s+=$1 END s’ myfile
“
Any method from Ruby Core and Active Support can be used. Rushed also provides some new methods to make transformations easier. Here are some variations on the above example:
“sh ru ‘map(:to_i, 10).sum’ myfile ru ‘map(:to_i).reduce(&:+)’ myfile ru ‘each_line.to_i.to_a.sum’ myfile ru ‘grep(/^\d+$/).map(:to_i).sum’ myfile ru ‘reduce(0) { |sum, n| sum + n.to_i }’ myfile ru ‘each_line.match(/(\d+)/)[1].to_i.to_a.sum’ myfile
“
See Examples and Methods for more.
Installation
“sh gem install rushed
“
You can now use Ruby in your shell!
For example, to sum a list of integers:
“sh $ echo “2\n3” | ru ‘map(:to_i).sum’ 5
“
Usage
See Examples below, too!
Rushed reads from stdin:
“sh $ echo “2\n3” | ru ‘map(:to_i).sum’ 5 $ cat myfile | ru ‘map(:to_i).sum’ 5
“
Or from file(s):
“sh $ ru ‘map(:to_i).sum’ myfile 5 $ ru ‘map(:to_i).sum’ myfile myfile 10
“
You can also run Ruby code without any input by prepending a !
:
“sh $ ru ‘! 2 + 3’ 5
“
In addition to the methods provided by Ruby Core and Active Support, Rushed provides other methods for performing transformations, like each_line
, files
, and grep
. See Methods for more.
Examples
Let’s compare the readability and conciseness of Rushed relative to existing tools:
Center lines
ru
“sh ru ‘map(:center, 80)’ myfile
“
awk
“sh awk ‘printf “%” int(40+length($0)/2) “s\n”, $0’ myfile
“
sed
Sum a list of integers
ru
“sh ru ‘map(:to_i).sum’ myfile
“
awk
“sh awk ‘s+=$1 END s’ myfile
“
paste
“sh paste -s -d+ myfile | bc
“
Print the 5th line
ru
“sh ru ‘[4]’ myfile
“
sed
“sh sed ‘5q;d’ myfile
“
Print all lines except the first and last
ru
“sh ru ‘[1..-2]’ myfile
“
sed
“sh sed ‘1d;$d’ myfile
“
Sort an Apache access log by response time (decreasing, with time prepended)
ru
“sh ru ‘map { |line| [line/(\d+)( .+)2$/, 1.to_i, line] }.sort.reverse.map(:join, “ ”)’ access.log
“
awk
“sh awk –re-interval ‘{ match($0, /((]+|[]+]|“+”)[[:space:]]+)7/, m); print m[2], $0 }’ access.log | sort -nk 1
“Source](https://coderwall.com/p/ueazhw)
Methods
In addition to the methods provided by Ruby Core and Active Support, Rushed provides other methods for performing transformations.
each_line
Provides a shorthand for calling methods on each iteration of the input. Best explained by example:
“sh ru ‘each_line.strip.center(80)’ myfile
“
If you’d like to transform it back into a list, call to_a
:
“sh ru ‘each_line.strip.to_a.map(:center, 80)’ myfile
“
files
Converts the lines to Rushed::File
objects (see Dotsch::File below).
“sh $ echo “foo.txt” | ru ‘files.map(:updated_at).map(:strftime, “”%Y-%m-%d“)’ 2014-11-08
“
format(format=‘l’)
Formats a list of Dotsch::File
s. You’ll typically call this after calling files
to transform them into strings:
“sh $ ru ‘files.format’ 644 tom staff 3 2014-10-26 09:06 bar.txt 644 tom staff 11 2014-11-04 08:29 foo.txt
“
The default format, 'l'
, is shown above. It prints [omode, owner, group, size, date, name]
.
grep
Selects lines which match the given regex.
“sh $ echo “john\npaul\ngeorge” | ru ‘grep(/o[h|r]/)’ john george
“
map
This is the same as Array#map
, but it adds a new syntax that allows you to easily pass arguments to a method. For example:
“sh $ echo “john\npaul” | ru ‘map(:[], 0)’ j p $ echo “john\npaul” | ru ‘map(:center, 8, “.”)’ ..john.. ..paul..
“
Note that the examples above can also be performed with each_line
:
“sh $ echo “john\npaul” | ru ‘each_line[0]’ $ echo “john\npaul” | ru ‘each_line.center(8, “.”)’
“
Testing
Nested Hstore is tested against ActiveRecord 3 and 4. If you’d like to submit a PR, please be sure to use Appraisal to test your changes in both contexts:
“sh appraisal rspec
“
License
Rushed is released under the MIT License. Please see the MIT-LICENSE file for details.