Skip to content

Instantly share code, notes, and snippets.

@mziwisky
Last active November 9, 2020 02:23
Show Gist options
  • Select an option

  • Save mziwisky/2fe40ba44d9996718a18a158f1b7200a to your computer and use it in GitHub Desktop.

Select an option

Save mziwisky/2fe40ba44d9996718a18a158f1b7200a to your computer and use it in GitHub Desktop.

Revisions

  1. mziwisky revised this gist Nov 9, 2020. 2 changed files with 12 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions main.rb
    Original file line number Diff line number Diff line change
    @@ -6,14 +6,18 @@
    output = ''
    status = Open3.popen2e('./sub.rb') do |std_in, std_out_err, wait_thr|
    Thread.new do
    while out = std_out_err.read(4)
    print(out); STDOUT.flush
    output += out
    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.read(4)
    while input = STDIN.readpartial(64)
    output += input
    std_in.write(input)
    end
    std_in.close
    4 changes: 4 additions & 0 deletions sub.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    #!/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)> "
  2. mziwisky revised this gist Nov 6, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions main.rb
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@

    require 'open3'

    puts 'party time'
    output = ''
    status = Open3.popen2e('./sub.rb') do |std_in, std_out_err, wait_thr|
    Thread.new do
  3. mziwisky created this gist Nov 6, 2020.
    24 changes: 24 additions & 0 deletions main.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    #!/usr/bin/env ruby

    require 'open3'

    output = ''
    status = Open3.popen2e('./sub.rb') do |std_in, std_out_err, wait_thr|
    Thread.new do
    while out = std_out_err.read(4)
    print(out); STDOUT.flush
    output += out
    end
    std_out_err.close
    end
    Thread.new do
    while input = STDIN.read(4)
    std_in.write(input)
    end
    std_in.close
    end
    wait_thr.value.exitstatus
    end

    puts "Output: #{output}"
    puts "Status: #{status}"
    9 changes: 9 additions & 0 deletions sub.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    #!/usr/bin/env ruby

    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