Created
April 22, 2013 23:46
-
-
Save benolee/5439579 to your computer and use it in GitHub Desktop.
simple example of using pipe, fork, and exec
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
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