Friday, February 26, 2010

How to Pipe in Ruby

Today I asked this on StackOverflow and it turns out that "piping is hard" was the only interesting thing that was said to Ruby's detriment for command-line scripting. How hard can it be?
So after a bit of spelunking, I got this to work:

which is Ruby for

Of course, there are more ways to pipe, including concurrent and sequential, but since I haven't understood those yet, I think I'm okay to move forward for now (and leave those on the todo list).

3 comments:

Paulo Lellis said...
This comment has been removed by a blog administrator.
Paulo Lellis said...

Thanks for the excellent tip!

I often use the %x macro to deal with pipes:

%x(find . | grep what)

but when it comes to named-piped or IO pipes, your's is killer!

Dan Rosenstark said...

Thank you! I've been out of Ruby for too long to be able to suggest how to do it, but I'd imagine you could take this further and use blocks/methods to make it even cleaner.