Created
November 1, 2014 13:09
-
-
Save emasaka/f79a6e5f3cd5e16faae8 to your computer and use it in GitHub Desktop.
shell-like pipeline in Ruby DSL (ver. 2)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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