News for hackers. Stuff that matters.
Re:Show HN: Rb – Turns Ruby into a command line utility by star-techate on Monday August 13, 2018 @11:47PM (#17755463)
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 | 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] } }
Bit of a pain as ++ is lacking, as is autovivication. a /bin/sort at the end usually beats `<=>` for terseness.
Re:Show HN: Rb – Turns Ruby into a command line utility by shawn on Tuesday August 14, 2018 @12:08AM (#17755530)
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 by star-techate on Tuesday August 14, 2018 @12:14AM (#17755549)
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 by asicsp on Tuesday August 14, 2018 @02:17AM (#17755924)
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'