Skip to content

Instantly share code, notes, and snippets.

@benolee
Created April 22, 2013 23:46
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 benolee/5439579 to your computer and use it in GitHub Desktop.
Save benolee/5439579 to your computer and use it in GitHub Desktop.
simple example of using pipe, fork, and exec
class Command
attr_accessor :pull, :push
def initialize(name, *args)
@name, @args, @pull, @push = name, args, *IO.pipe
end
def exec
fork do
pull.close
$stdout.reopen(push)
Kernel.exec @name, *@args
end
push.close
pull.read.split("\n")
end
end
touch = Command.new("touch", "tmp/foobar")
ls = Command.new("ls", "-ahl", "tmp")
touch.exec
ls.exec.grep /ba/
#=> ["-rw------- 1 u40141 40141 0 2013-04-22 23:43 foobar"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment