Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 650949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:06:33+00:00 2026-05-13T22:06:33+00:00

I am trying to use IO.popen in order to put (with .puts method) and

  • 0

I am trying to use IO.popen in order to put (with .puts method) and to get (with .gets method) messages from a process to its sub-process.

I am not very experimented and I have a question about. Having the following code, I have an error because it is not possible to write in a closed stream.

class Interface
  def initialize(path)
    @sub_process = IO.popen(path, 'w+')
  end

  def start!
    if ok?
      @sub_process.puts 'Hello', 'my name is ...'
      # and more...
    end
  end

  protected

  def ok?
    is_ready?(@sub_process) && is_cool?(@sub_process)
  end

  def is_ready?(sub_process)
    reply = process_command(sub_process, 'are u ready?')
    reply.chomp.match(/yes_i_am_ready$/)
  end

  def is_cool?(sub_process)
    reply = process_command(sub_process, 'are u cool?')
    reply.chomp.match(/yes_i_am_cool$/)
  end

  def process_command(sub_process, command)
    rdr = Thread.new { sub_process.read } # alternative: io.readlines
    sub_process.puts "#{command}"
    sub_process.close_write
    rdr.value # joins and fetches the result
  end
end

a = Interface.new("./program")
a.start!

(…) in `write’: not opened for writing (IOError)

As we can see, this error occur during is_cool? test (as explained at: http://ruby-doc.org/core/classes/IO.html#M002289).

But if I try to comment in process_command method the line:

# sub_process.close_write

the script seems to sleep… infinitely :s

I believe that it is not possible to open again a closed stream. And I can’t create an other IO.popen instance of my program “./program” because it needs to be initialized with some command (like ‘are u ready?’ and ‘are u cool?’) at the beginning, before I use it (by sending and receiving messages like a simple discussion).

How changes can I do over the current code in order to solve this problem?

Edit: in other words, I would like to establish a such communication (according to a given protocol):

Parent message:                Child answer:
--------------                 ------------

'are u ready?'                 'yes_i_am_ready'
'are u cool?'                  'yes_i_am_cool'
'Hello'                        'foo'
'my name is ...'               'bar'

Many thanks for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:06:33+00:00Added an answer on May 13, 2026 at 10:06 pm

    Perhaps it will help to have a working example. Here’s one, tested and known to work in MRI 1.8.7 on Linux.

    bar.rb

    #!/usr/bin/ruby1.8
    
    begin
      loop do
        puts "You said: #{gets}"
        $stdout.flush
      end
    rescue Errno::EPIPE
    end
    

    foo.rb

    #!/usr/bin/ruby1.8
    
    class Parent
    
      def initialize
        @pipe = IO.popen(CHILD_COMMAND, 'w+')
      end
    
      def talk(message)
        @pipe.puts(message)
        response = @pipe.gets
        if response.nil?
          $stderr.puts "Failed: #{CHILD_COMMAND}"
          exit(1)
        end
        response.chomp
      end
    
      private
    
      CHILD_COMMAND = './bar.rb'
    
    end
    
    parent = Parent.new
    puts parent.talk('blah blah blah')
    puts parent.talk('foo bar baz')
    

    foo.rb output

    You said: blah blah blah
    You said: foo bar baz
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use subprocess.Popen to construct a sequence to grab the duration of
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
I am trying use a Java Uploader in a ROR app (for its ease
Trying to use this method (gist of which is use self.method_name in the FunnyHelper
I am trying to use th subprocess module along with the Popen class to
I am trying to get the mercurial revision number/id (it's a hash not a
I am trying use std::copy to copy from two different iterator. But during course
i'm trying use webview to load a image from sdcard i use this path
I'm trying to use popen() to catch the stderr of a call, but of
I am trying to untar a tar file via popen ( from platform import

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.