Earlier quoted context omitted.
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.
Show HN: Rb – Turns Ruby into a command line utility
41–50 of 79 posts
Re: Show HN: Rb – Turns Ruby into a command line utility
#42Interesting, 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.
Each of your examples use different flags, inputs and print methods. The last is particularly good at proving the point!
Re: Show HN: Rb – Turns Ruby into a command line utility
#43Re: Show HN: Rb – Turns Ruby into a command line utility
#44I'd love this for python as I'm not a big fan of Ruby. Does such a thing exist?
Re: Show HN: Rb – Turns Ruby into a command line utility
#45Re: Show HN: Rb – Turns Ruby into a command line utility
#46All 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.
Sure, you can do all that with bash. But you can also make system() calls with Ruby, with full string interpolation power tools. If you're a Rubyist, and you want to do data analysis, this is the workflow you want.
Re: Show HN: Rb – Turns Ruby into a command line utility
#47The 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'm wanting a pipeline, I'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. All things perfectly manageable inside a PORO. Bundler even has an inline mode, so you can put everything i…
Re: Show HN: Rb – Turns Ruby into a command line utility
#48The 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'm wanting a pipeline, I'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. All things perfectly manageable inside a PORO. Bundler even has an inline mode, so you can put everything i…
Re: Show HN: Rb – Turns Ruby into a command line utility
#49The command should process stdin in streaming fashion rather than slurping it all at once: code = ARGV[0] STDIN.each_line do |l| puts l.instance_eval(code) STDOUT.flush end
OP apparently wants to process an array of lines, not just each line separately.
Re: Show HN: Rb – Turns Ruby into a command line utility
#50$ 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