Live data from Hacker News

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

github.com

21–30 of 79 posts

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

#21
post #3
post #2

I have no idea what this is doing, but it sure looks interesting.

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

Why the hell would anybody want to replace AWK? It’s super fast, extremely light on resources, doesn’t have any dependencies, does auto memory management and type inference, and is extremely powerful for large dataset processing, not to mention easy to learn and program.

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

#24
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.

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

Does it, though?

  ps | ruby -le 'b = $
or:

  ps | ruby -lane 'b = (b || 0) + $F[0].to_i; END { print "sum of PIDs: #{b}" }'

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

#26

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…

for the 3rd example, not sure why you are manually building the array..

    df -h | ruby -e 'puts readlines.drop(1).sort_by { |l| l.split[-2].to_f }'
similarly, the `uniq` example from your other comment could be shortened to

    printf 'hello\nworld\nhello\n' | ruby -e 'puts readlines.uniq'

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

#27

$ docker ps | rb drop 1 | rb -l split[1] $ docker ps | perl -anE 'say $F[1] if $.>1' perl solved this problem a long time ago, people

for your given sample, you could use awk[0] as well

    awk 'NR>1{print $2}'
or ruby[1]

    ruby -ane 'puts $F[1] if $.>1'
which one to use depends on lots of factor - speed, features, availability, etc as well as whether user already knows perl[2]/ruby/etc

Further reading(disclosure: I wrote these)

[0]: https://github.com/learnbyexample/Command-line-text-processi...

[1]: https://github.com/learnbyexample/Command-line-text-processi...

[2]: https://github.com/learnbyexample/Command-line-text-processi...

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

#29
post #22

Earlier quoted context omitted.

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

Right tool for the job; don't be a fan boy.

As someone who prefers Python to Ruby, Ruby is better for this job because there are so many more methods on Array/Enumerable than on Python list (or Python's global functions.) And them all being methods makes life simpler too.
Post reply on HN