On 01/02/2010 03:33 AM, Shay Hawkins wrote:
> Ryan Davis wrote:
>> ri StringIO
> 
> Thanks, but I think I'm missing something:
> 
>> test.rb:15:in 'select': can't convert StringIO to IO (TypeError)
> 
> objectA = StringIO.new("")
> objectB = StringIO.new("")
> ..
> ready = select([objectA,objectB])
> 
> What would be the alternative to using select on them?
> 
> I apologize if these are basic questions, but I've tried Google and 
> looked on both the IO and StringIO documentation pages with no luck. If 
> there's a better way for me to go about searching, do tell - I'm all for 
> nagging you guys less.

If your scenario is such that for any particular thread you are always 
either reading *or* writing then you should probably look at class Queue.

require 'thread'

io = Queue.new # well, this isn't an IO

writers = (1..5).map do
   Thread.new do
     rand(10).times do |i|
       io.enq i
     end
   end
end

readers = (1..2).map do
   Thread.new do
     while (x=io.deq)
       puts x
     end
   end
end

writers.each {|th| th.join}
readers.size.times {io.enq nil}
readers.each {|th| th.join}

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/