Skip to content

Instantly share code, notes, and snippets.

@mziwisky

mziwisky/main.rb

Last active Nov 9, 2020
Embed
What would you like to do?
proxying I/O from one script to another
#!/usr/bin/env ruby
require 'open3'
puts 'party time'
output = ''
status = Open3.popen2e('./sub.rb') do |std_in, std_out_err, wait_thr|
Thread.new do
begin
while out = std_out_err.readpartial(64)
print(out); STDOUT.flush
output += out
end
rescue EOFError
end
std_out_err.close
end
Thread.new do
while input = STDIN.readpartial(64)
output += input
std_in.write(input)
end
std_in.close
end
wait_thr.value.exitstatus
end
puts "Output: #{output}"
puts "Status: #{status}"
#!/usr/bin/env ruby
# this is important! I don't know of a way that it could be set
# from main.rb, e.g. if you only had control of main.rb and not sub.rb
STDOUT.sync = true
puts "it's a test!"
loop do
print "gimme something (q to quit)> "
x = gets.chomp
puts "you gave me #{x}"
exit if x == 'q'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.