Live data from Hacker News

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

github.com

41–50 of 79 posts

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

#41
post #21
post #3

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.

I want to say that Perl, and Ruby, and even Python are superior tools .... except that awk is also my default go-to for pipiing into to munge output data, so yes it's clearly still hitting the sweet spot for many tasks.

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

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

If this was an attempt at showing this tool is unnecessary, you just did the opposite.

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

#44
post #15

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

Several people have tried to tackle this in Python. My attempt was python-oneliner[1]. There's also a list of similar projects in the readme.

[1] https://github.com/gvalkov/python-oneliner

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

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

#47

The 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…

I hadn't seen bundler-inline before, thanks for posting this. That'll be nice for small scripts that don't warrant a whole directory themselves, or useful for a sort of "tools" / "utils" repo with small utility scripts.

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

#48

The 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…

xargs -P and other ad-hoc parallelism tools usually stop me from leaving the command line too quickly (e.g. The other thing that keeps me back is scale. If everything fits in memory, cool, but sometimes I need to sort or uniq something that won't.

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

#49
post #17

The 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.

We need to be using lazy coolections.

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

Except that in your example, the first line is coherent English, and the second line is just... well... code.
Post reply on HN