Live data from Hacker News

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

github.com

11–20 of 79 posts

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

#11

Command line tools in 10 lines of ruby (using this script): 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 }' Command line tools in zero lines of ruby (using ruby): docker ps | ruby -lane 'next if $. == 1; print $F[1] docker ps -a | ruby -lne 'print $1 if /(Exited .*? ago)/' df | rub…

In case it’s helpful, as someone who doesn't know ruby, it's hard to understand. Not too readable. Split.last.ljust I just barely understood (left-justify the last match), but I have no idea what the next part is even trying to accomplish.

It's a cool script though.

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

#13
post #9

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. A few examples: Capitalizing a line: echo hello world | rb -l capitalize Printing only unique lines: printf 'hello\nw…

    echo hello world | ruby -pe '$_.capitalize!'

    printf 'hello\nworld\nhello\n' | ruby -le 'puts STDIN.to_a.uniq

    ps | ruby -lane 'BEGIN { b = 0 }; b += $F[0].to_i; END { print "sum of PIDs: #{b}" }
select whatever column you want, to sum up.

Really what Ruby needs is a flag that adds some capabilities to NilClass, so that these BEGIN blocks aren't needed.

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

#14
post #11

Command line tools in 10 lines of ruby (using this script): 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 }' Command line tools in zero lines of ruby (using ruby): docker ps | ruby -lane 'next if $. == 1; print $F[1] docker ps -a | ruby -lne 'print $1 if /(Exited .*? ago)/' df | rub…

In case it’s helpful, as someone who doesn't know ruby, it's hard to understand. Not too readable. Split.last.ljust I just barely understood (left-justify the last match), but I have no idea what the next part is even trying to accomplish. It's a cool script though.

That's the author's oneliner. I don't know what that code is for either, which is part of I rewrote it in the second set of examples.

It's normal for a oneliner to be somewhat inscrutable though, since they're often extremely dependent on their inputs. Like if it's important to fetch the 3rd, 5th, and 7th column--why? You can't tell without the input.

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

#16
post #10
post #3

Earlier quoted context omitted.

It's using Ruby to replace Awk, the way Perl once tried to.

Technically Ruby tried to replace Perl, so I guess the shoe fits. It sure has taken a while for someone to do this!

Well the thing is, matz did it a decade ago when he carefully copied all these features that Perl came up with or itself carefully copied from awk and sed.

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

#20
post #15

I'd love this for python as I'm not a big fan of Ruby. Does such a thing exist?

Out of interest, why are you not a fan of Ruby?

Not the parent but speaking myself, I feel the need to pick one or the other.

I'm not saying I hate Ruby. I just haven't take the time (nor had a reason to, frankly) become a fan the way I have with Python. Being similar languages in terms of both being dynamic, malleable, mostly single threaded and REPL-able they both seem to occupy the same spot in my toolbox.

Not to knock Ruby-the-language, but the one hitch is that Python's ecosystem is quite a bit vaster into fields I don't interact with (biology, physics, etc.) but also with many that I do (numpy/sympy, cli utilities and scripting, opencv, curl bindings). Where I'm at, it's most everyone's secondary or tertiary language.

Nothing against Ruby, it's just circumstance. If anything I might pick it up soon if only to access JRuby/TruffleRuby worlds which are much ahead of Jython/GraalPython.

Oh, except for function calls without (). And I'm not huge on DSLs. They seem icky but I grew to love significant whitespace in Python so maybe I'll grow to love those too.

Post reply on HN