Skip to content

Instantly share code, notes, and snippets.

@knzai
Created February 25, 2010 02:59
Show Gist options
  • Save knzai/314180 to your computer and use it in GitHub Desktop.
Save knzai/314180 to your computer and use it in GitHub Desktop.
#http://github.com/ahoward/open4 is an alternative that handles pid and exit status
#http://github.com/ahoward/session a more fleshed our shell utility
require 'open3'
Open3.popen3("dc") do |stdin, stdout, stderr|
t = Thread.new(stderr) do |terr|
while (line = terr.gets)
puts "stderr: #{line}"
end
end
puts "pushing 5 to stack"
stdin.puts(5)
puts "pushing 10 to stack"
stdin.puts(10)
puts "pushing + to stack"
stdin.puts("+")
puts "sending print command"
stdin.puts("p")
result = stdout.gets
puts "stdout: #{ result }"
puts "\ntrying an unsupported command"
stdin.puts("b")
extra_info_not_in_stderror = stdout.gets
puts "stdout: #{extra_info_not_in_stderror}"
puts "\nclosing stdin to avoid deadlock with stderr"
stdin.close
# puts "let's give stderr a chance to run"
# t.run or sleep 1
puts "joining"
t.join
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment