Witam.
Zaciekawił mnie jeden problem. A mianowicie, jak napiszę irc bota w ten sposób:
[code]require “socket”
class IRCbot
def initialize(server, port, nick, channel)
@server = server
@port = port
@nick = nick
@channel = channel
end
def connect
@socket = TCPSocket.open(@server, @port)
@socket.puts "USER blah blah blah :blah blah"
puts "[ Send: USER blah blah blah :blah blah]"
@socket.puts "NICK #{@nick}"
puts "[ Send: NICK #{@nick} ]"
sleep 20
@socket.puts "JOIN #{@channel}"
puts "[ Send: JOIN #{@channel} ]"
@socket.puts "AWAY :IRC test bot by Imag0. [Ruby]"
end
def socket_receive
while true
sock = @socket.gets
if sock =~ /^PING\s:(.+)$/ then
puts "[ Server ping ]"
@socket.gets "PONG :#{$1}"
end
end
end
def puts_socket_receive
while true
putssock = @socket.gets
puts "#{putssock}"
end
end
end
bot = IRCbot.new(‘localhost’, 8888, ‘IRCnick’, ‘#chan’)
bot.connect
bot.puts_socket_receive
bot.socket_receive[/code]
To pętla w socket_receive się nie wykona, a wykona się ta w puts_socket_receive.
Jeśli zakomentuję całą definicję puts_socket_receive, to pętla z socket_receive się wykona.
Dlaczego tak jest? I jak temu zapobiec?