Skip to content

Instantly share code, notes, and snippets.

@emasaka
Created November 1, 2014 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emasaka/f79a6e5f3cd5e16faae8 to your computer and use it in GitHub Desktop.
Save emasaka/f79a6e5f3cd5e16faae8 to your computer and use it in GitHub Desktop.
shell-like pipeline in Ruby DSL (ver. 2)
#!/usr/bin/env ruby
require 'open3'
module PipeOperator
refine Array do
def |(x)
PipeLine.new(self) | x
end
end
refine String do
def |(x)
PipeLine.new(self) | x
end
end
end
class PipeLine
def initialize(cmd)
@cmds = [cmd]
end
def |(cmd)
@cmds << (Array === cmd ? cmd : [cmd])
self
end
def execute
Open3.pipeline(*@cmds)
end
def self.run
yield.execute
end
end
if __FILE__ == $0
using PipeOperator
PipeLine::run { ['yes', 'Yo!'] | 'nl' | ['head', '-n', '10'] }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment