Interesting, so it's reading text from STDIN into an Array, then executing its arguments as Ruby code in the context of that Array (or if you pass -l, in the context of each line individually). So all the standard methods on the [Array][1] (or [String][2]) class become accessible as top-level methods.<p>A few examples:<p>Capitalizing a line:<p><pre><code> echo hello world | rb -l capitalize
</code></pre>
Printing only unique lines:<p><pre><code> printf 'hello\nworld\nhello\n' | rb uniq
</code></pre>
Computing the sum of numbers in a table:<p><pre><code> printf '1\n2\n3\n' | rb 'map(&:to_i).sum'
</code></pre>
Personally I think it's a really cool idea.<p>[1]: <a href="https://ruby-doc.org/core/Array.html" rel="nofollow">https://ruby-doc.org/core/Array.html</a><p>[2]: <a href="https://ruby-doc.org/core/String.html" rel="nofollow">https://ruby-doc.org/core/String.html</a>
Command line tools in <i>10 lines of ruby</i> (using this script):<p><pre><code> docker ps | rb drop 1 | rb -l split[1]
docker ps -a | rb grep /Exited/ | rb -l 'split.last.ljust(20) + " => " + split(/ {2,}/)[-2]'
df -h | rb 'drop(1).sort_by { |l| l.split[-2].to_f }'
</code></pre>
Command line tools in <i>zero lines of ruby</i> (using ruby):<p><pre><code> docker ps | ruby -lane 'next if $. == 1; print $F[1]
docker ps -a | ruby -lne 'print $1 if /(Exited .*? ago)/'
df | ruby -lane 'BEGIN { lines = [] }; lines.push [$F[4].to_i, $_] if $. > 0; END { lines.sort { |a,b|b[0] <=> a[0] }.each{|k| print k[1] } }
</code></pre>
Bit of a pain as ++ is lacking, as is autovivication. a /bin/sort at the end usually beats `<=>` for terseness.
The command should process stdin in streaming fashion rather than slurping it all at once:<p><pre><code> code = ARGV[0]
STDIN.each_line do |l|
puts l.instance_eval(code)
STDOUT.flush
end</code></pre>
It makes me a little uncomfortable that they're using curl|bash for something as simple as "put this 10-line script somewhere in your $PATH," especially when the script involves sudo (to move into /usr/local/bin). Sure, it's easy to inspect the script and see that it's not doing anything malicious, but it makes install processes like this, where it'd be incredibly easy to, seem normal.
The second I start wanting a bash pipeline, probably around the third pipe, I scrap it immediately and move to using a text editor to write a script. Because if I'm wanting a pipeline, I'm also going to want to store it, and transmit it over the network, and manage it, and put it down and come back to it later.<p>All things perfectly manageable inside a PORO. Bundler even has an inline mode, so you can put everything in one file, close to your data, Bundler just handles dep management under the hood like a boss. Check out bundler-inline.<p>Sure, you <i>can</i> do all that with bash. But you can also make system() calls with Ruby, with full string interpolation power tools. If you're a Rubyist, and you want to do data analysis, this is the workflow you want.
<p><pre><code> $ docker ps | rb drop 1 | rb -l split[1]
$ docker ps | perl -anE 'say $F[1] if $.>1'
</code></pre>
perl solved this problem a long time ago, people
What does this have over the default ruby?<p>cat your-file.txt | ruby -ne '$_.your_ruby_stuff'<p>Just syntactic sugar? (it does look cleaner!)
"pyped" does the same for python.<p>I though it was great, knowing python better than bash, plus it was portable to windows.<p>But eventually I always end up using built in GNU tools.<p>Don't know why. It just rolls better.
“With 10 lines of Ruby replace most of the command line tools that you use to process text inside of the terminal.”<p>Well, it’s these 10 lines, plus an unlimited amount of Ruby that you have to compose on the fly for each operation.
That's similiar to the ruby-each-line gem I created <a href="https://github.com/Dorian/ruby-each-line" rel="nofollow">https://github.com/Dorian/ruby-each-line</a>
First I thought: This is stupid.
But then I watched the examples on github and here in the comments, and I changed my mind: It's pretty neat :) I like it.
I found the last example hard to read so I made this fork : <a href="https://github.com/kimat/rb" rel="nofollow">https://github.com/kimat/rb</a>
- Reading all the lines into an array is a big error.<p>- It's a bit unintiuitive because you don't really know what strings to escape on the bash command line. In the example, some are escaped and others aren't.<p>- In the end I think this could be achieved with a simple bash function:<p><pre><code> $ rb() { ruby -lane "print \$_.instance_eval(\"$@\")"; }
$ echo hello | rb capitalize
$ Hello</code></pre>