Skip to content

Instantly share code, notes, and snippets.

@cwoodall
Created July 22, 2011 02:30
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 cwoodall/1098766 to your computer and use it in GitHub Desktop.
Save cwoodall/1098766 to your computer and use it in GitHub Desktop.
A nice little (very rough and being matured) wrapper for reading output asynchronously in ruby using PTY (built-in PseudoTerminal Library). I plan on expanding on this.
# Author :: Christopher Woodall <chris.j.woodall@gmail.com>
# Copyright :: 2011 Christopher Woodall
# License :: MIT License
require 'pty'
# run_proc(proc) runs a psuedoterminal version of a command returning output to a block
# asynchronously as it is produced.
def run_proc(proc)
# Spawn the PTY process
PTY.spawn(proc) do |r, w, pid|
# Loop through the process until the end of file is reached
# and yeild to the block whenever a line is read
loop do
begin
yield r.readline
rescue EOFError
break
end
end
end
end
# Example
run_proc("ruby meow.rb") do |a|
puts a
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment