The ruby way of interrupting a Kernel::select?

Hello everyone,

As a newbie with too much java-experience, I found it strange that I couldn't interrupt the Kernel::select.

Basically what I wanted to do would be to interrupt the select as soon as a socket decided it wanted to write something, and then add that channel to the write-interest array.

I found no obvious way of doing that, so I made a hack around it: I registered a pipe with the read-interest and every time I wanted to write something then I would send a single byte on the pipe, which would interrupt the select and allow me to register the socket with the array.

I.e. something like this:


  @notifier, @requester = IO.pipe # this is my hack
    puts "Server started on port #{port} with handler #{@delegate}."
    while (true)
      @read = [@server, @notifier]
      @write =
      @error = [@server]
      @connections.each do |connection|
        @read << if
        @write << if connection.write
        @error << if
      read, write, error = Kernel::select(@read, @write, @error)
      @connections.each do |connection|
        connection.handle_read if read.include?
        connection.handle_write if write.include?
        connection.handle_error if error.include?
      @notifier.getc if read.include? @notifier
      handle_connect if read.include? @server

  def poke
    #this is a hack to interrupt Kernel::select
    puts "Poking!"
    @requester.putc "!"

Please tell me there's a better way! :slight_smile: