Live data from Hacker News

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

github.com

51–60 of 79 posts

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

#51
post #15

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

Not well tested... just whipped this up.

The example to sort df -h by the percent:

  df -h | p 'sorted({}[1:], key=lambda x: int(x.split()[-2][:-1]))'
I saved this as ~/bin/p

  #!/usr/bin/env python3
  
  import sys
  
  single_line = sys.argv[1] == '-l'
  code = ' '.join(sys.argv[2 if single_line else 1:])
  if single_line:
      for line in sys.stdin:
          print(eval(code.format('line.strip()')))
  else:
    print('\n'.join(eval(code.format('sys.stdin.read().splitlines()'))))

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

#52

Earlier quoted context omitted.

OP apparently wants to process an array of lines, not just each line separately.

We need to be using lazy coolections.

So:

  execute(STDIN.each_line, code)
which gives you a lazy enumerator, in place of:

  execute(STDIN.readlines, code)
which gives you an Array.

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

#53
post #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.

I agree. I love Ruby, but I'll pipe 'httpie', 'jq' and 'xargs' for quite awhile before deciding to write a Ruby script to do something similar.

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

#55
post #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.

Yeah I suppose my attitude will change should I ever start needing to do heavy lifting. But I'll definitely explore instrumenting bash through ruby first, just to see where that line lies.

There's also the option of pushing the workload off onto AWS that I may look into.

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

#56
post #34

$ 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

Yeah, just like eating dirt solved the problem of world hunger. Look again at your code. I'm not 100% sure what the Ruby version does, but once I do figure out the exact semantics of drop and split, it's going to be waaaaaaaaaaaaaaaaay easier to remember, understand and modify the Ruby version than the Perl one. Your post comes off as something from reddit's /r/nottheonion, you're just making the case against Perl fo…

The perl here is not really that crazy to remember.

-a autosplits each line by whitespace and puts each element into the array F (this was inspired by AWK).

-n loops through each line of the file, and -E executes the perl.

$. (NR in AWK) is the line number.

As others have noted, you can write the same thing in ruby on the command line already with `ruby -ane 'puts $F[1] if $.>1'`

(Notice the similarities?)

If you write one liners in AWK, Perl, or Ruby often the "odd" variables look more like useful shortcuts.

*edit You could also write the perl without any of the special variables, but it would be much more verbose, hence the special characters and flags.

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

#58

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…

Don't you loose the easy threading that's inherent in a pipeline of processes though?

I love ruby but for throw away jobs I'd be more likely to do it on the command line than a ruby script simply because it would be quicker for me to write and run in a shell. Unless I'm wrong at the very least you wouldn't have to deal with the GIL.

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

#59

$ 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

but the ruby code is much more intuitively readable

It is if you know ruby. Me, I know perl and honestly I do not grok most of the ruby examples given here. I am quite sure that I could learn them about as fast as somebody not knowing perl and knowing ruby could learn perl.
Post reply on HN