eastcoastcoder / gmail.com wrote:
> Anyone know of any generic implementation of a pipeline in ruby?  That
> is, one which takes a bunch of GoF Command objects, and strings them
> together in the classical pipeline configuration.
>
> Any advice on that?
>
> I have a process with a lot of sequential processing, and think that a
> pipeline of Command objects (not just Proc's) might be the way to do
> it.
>
> The downsides of pipelines:
> 1) You can't do selections (if) or sequences (loops).  If you can
> handle this, though, this becomes a virtue, as it keeps everything
> crystal clear and simple.  Witness the ever useful Unix command line
> pipelines.
>
> 2) They don't encapsulate very well, as each stage totally overwrites
> the previous ones.  I'm not sure if I should just accept this, or try
> to use some modification (append only pipeline, etc.).
>
> Any experience or ideas on this appreciated.

There are several ways you can do pipelining.  Do you want concurrency
with that?  Does every stage have a single output the next stage wants to
operate on?  etc.

Probably the simplest thing you can do is to use #inject:

commands = [
  lambda {|x| x * 2},
  lambda {|x| 0 - x},
  lambda {|x| x + 10},
]

>> commands.inject(0) {|x,cmd| cmd[x]}
=> 10
>> commands.inject(1) {|x,cmd| cmd[x]}
=> 8

Kind regards

    robert