Live data from Hacker News

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

github.com

1–10 of 79 posts

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

#4
post #2

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

It's feeding stdin to ruby code you specify on the command line, either as an array of lines, or repeated strings. So in:

  docker ps | rb drop 1 | rb -l split[1]
The output of docker ps is passed as an array of lines to ruby, and the first line is dropped, and then that output is fed to ruby again, but line by line, so each line is split (which defaults to split on whitespace), and returns the second element. With parentheses for function calls it would be:

  docker ps | rb drop(1) | rb -l split()[1]

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

#6
post #2

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

It's feeding stdin to ruby code you specify on the command line, either as an array of lines, or repeated strings. So in: docker ps | rb drop 1 | rb -l split[1] The output of docker ps is passed as an array of lines to ruby, and the first line is dropped, and then that output is fed to ruby again, but line by line, so each line is split (which defaults to split on whitespace), and returns the second element. With par…

So drop is just a Ruby function? This is a great idea, seems like a good application of Ruby's syntax.

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

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

#8
post #5

What does this have over the default ruby? cat your-file.txt | ruby -ne '$_.your_ruby_stuff' Just syntactic sugar? (it does look cleaner!)

My guess is that the author was unaware of those flags. matz's idea that Ruby be a worthy successor to Perl was sort of crushed by Java weenies when the Pickaxe book came out.

I know this is ancient history.

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

#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\nworld\nhello\n' | rb uniq
Computing the sum of numbers in a table:

   printf '1\n2\n3\n' | rb 'map(&:to_i).sum'
Personally I think it's a really cool idea.

[1]: https://ruby-doc.org/core/Array.html

[2]: https://ruby-doc.org/core/String.html

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

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

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