I'm trying to use select on STDIN and a TCP socket in Ruby, but for some reason, the value returned from select never seems to match one of the choices; it looks like it's the socket that's being returned, but it doesn't match using == (or equal?). Can anyone tell me why the result returned from select doesn't match the objects I passed in, and what I should be doing differently here?
server = TCPSocket::new("irc.freenode.net", 7000)
server.puts "NICK MyBot"
server.puts "USER #{ENV['USER']} 0 * :My Bot"
# <snip definitions>
while (!$done)
results = select([server, STDIN], nil, nil)
if results[0] == STDIN
puts "Reading from STDIN"
execute_command
elsif results[0] == server
puts "Reading from server"
receive_data
else
puts "Something's wrong... results[0]: #{results[0]}, server: #{server}"
puts "IDs: results[0]: #{results[0].__id__}, server: #{server.__id__}"
exit 1
end
end
Here's what I get when I run this:
Something's wrong... results[0]: #<TCPSocket:0x33c390>, server: #<TCPSocket:0x33c390> IDs: results[0]: 1695840, server: 1695990
I'm running Ruby version 1.8.6 on Mac OS X.
$ ruby --version ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0] $ which ruby /usr/bin/ruby