TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: Rb – Turns Ruby into a command line utility

232 pointsby redkaalmost 7 years ago

19 comments

Ajedi32almost 7 years ago
Interesting, so it&#x27;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 &#x27;hello\nworld\nhello\n&#x27; | rb uniq </code></pre> Computing the sum of numbers in a table:<p><pre><code> printf &#x27;1\n2\n3\n&#x27; | rb &#x27;map(&amp;:to_i).sum&#x27; </code></pre> Personally I think it&#x27;s a really cool idea.<p>[1]: <a href="https:&#x2F;&#x2F;ruby-doc.org&#x2F;core&#x2F;Array.html" rel="nofollow">https:&#x2F;&#x2F;ruby-doc.org&#x2F;core&#x2F;Array.html</a><p>[2]: <a href="https:&#x2F;&#x2F;ruby-doc.org&#x2F;core&#x2F;String.html" rel="nofollow">https:&#x2F;&#x2F;ruby-doc.org&#x2F;core&#x2F;String.html</a>
评论 #17755541 未加载
star-techatealmost 7 years ago
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 &#x2F;Exited&#x2F; | rb -l &#x27;split.last.ljust(20) + &quot; =&gt; &quot; + split(&#x2F; {2,}&#x2F;)[-2]&#x27; df -h | rb &#x27;drop(1).sort_by { |l| l.split[-2].to_f }&#x27; </code></pre> Command line tools in <i>zero lines of ruby</i> (using ruby):<p><pre><code> docker ps | ruby -lane &#x27;next if $. == 1; print $F[1] docker ps -a | ruby -lne &#x27;print $1 if &#x2F;(Exited .*? ago)&#x2F;&#x27; df | ruby -lane &#x27;BEGIN { lines = [] }; lines.push [$F[4].to_i, $_] if $. &gt; 0; END { lines.sort { |a,b|b[0] &lt;=&gt; a[0] }.each{|k| print k[1] } } </code></pre> Bit of a pain as ++ is lacking, as is autovivication. a &#x2F;bin&#x2F;sort at the end usually beats `&lt;=&gt;` for terseness.
评论 #17755530 未加载
评论 #17755924 未加载
pulissealmost 7 years ago
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>
评论 #17756370 未加载
评论 #17756316 未加载
KeyboardFirealmost 7 years ago
It makes me a little uncomfortable that they&#x27;re using curl|bash for something as simple as &quot;put this 10-line script somewhere in your $PATH,&quot; especially when the script involves sudo (to move into &#x2F;usr&#x2F;local&#x2F;bin). Sure, it&#x27;s easy to inspect the script and see that it&#x27;s not doing anything malicious, but it makes install processes like this, where it&#x27;d be incredibly easy to, seem normal.
评论 #17759533 未加载
评论 #17759461 未加载
评论 #17759607 未加载
vinceguidryalmost 7 years ago
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&#x27;m wanting a pipeline, I&#x27;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&#x27;re a Rubyist, and you want to do data analysis, this is the workflow you want.
评论 #17756973 未加载
评论 #17789998 未加载
评论 #17757222 未加载
评论 #17758655 未加载
perlpersonalmost 7 years ago
<p><pre><code> $ docker ps | rb drop 1 | rb -l split[1] $ docker ps | perl -anE &#x27;say $F[1] if $.&gt;1&#x27; </code></pre> perl solved this problem a long time ago, people
评论 #17755953 未加载
评论 #17756236 未加载
评论 #17757418 未加载
评论 #17756088 未加载
评论 #17758968 未加载
meow_mixalmost 7 years ago
What does this have over the default ruby?<p>cat your-file.txt | ruby -ne &#x27;$_.your_ruby_stuff&#x27;<p>Just syntactic sugar? (it does look cleaner!)
评论 #17755595 未加载
评论 #17756298 未加载
评论 #17755471 未加载
sametmaxalmost 7 years ago
&quot;pyped&quot; 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&#x27;t know why. It just rolls better.
code_duckalmost 7 years ago
“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.
nerdponxalmost 7 years ago
I have no idea what this is doing, but it sure looks interesting.
评论 #17755387 未加载
评论 #17755390 未加载
dorianmalmost 7 years ago
That&#x27;s similiar to the ruby-each-line gem I created <a href="https:&#x2F;&#x2F;github.com&#x2F;Dorian&#x2F;ruby-each-line" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Dorian&#x2F;ruby-each-line</a>
tobyhinloopenalmost 7 years ago
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&#x27;s pretty neat :) I like it.
binaryphilealmost 7 years ago
A bashtardization:<p><pre><code> rb () { [[ $1 == -l ]] &amp;&amp; shift case $? in 0 ) ruby -e &quot;STDIN.each_line { |l| puts l.chomp.instance_eval(&amp;eval(&#x27;Proc.new { $* }&#x27;)) }&quot;;; * ) ruby -e &quot;puts STDIN.each_line.instance_eval(&amp;eval(&#x27;Proc.new { $* }&#x27;))&quot;;; esac }</code></pre>
robaxalmost 7 years ago
I&#x27;d love this for python as I&#x27;m not a big fan of Ruby. Does such a thing exist?
评论 #17757470 未加载
评论 #17756673 未加载
评论 #17758472 未加载
评论 #17764644 未加载
评论 #17755578 未加载
kimatalmost 7 years ago
I found the last example hard to read so I made this fork : <a href="https:&#x2F;&#x2F;github.com&#x2F;kimat&#x2F;rb" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kimat&#x2F;rb</a>
codetheoryalmost 7 years ago
Cool.
codetheoryalmost 7 years ago
Nice
jaequeryalmost 7 years ago
its a pretty nifty idea
tigreznoalmost 7 years ago
- Reading all the lines into an array is a big error.<p>- It&#x27;s a bit unintiuitive because you don&#x27;t really know what strings to escape on the bash command line. In the example, some are escaped and others aren&#x27;t.<p>- In the end I think this could be achieved with a simple bash function:<p><pre><code> $ rb() { ruby -lane &quot;print \$_.instance_eval(\&quot;$@\&quot;)&quot;; } $ echo hello | rb capitalize $ Hello</code></pre>