Simple socketing

Hi there, I recently started with Ruby and I am trying to make a very
simple irc bot, but I am having some slight problems though.

This is what I came up with:

require ‘socket’
class Irc
def connect(server, port = 6667)
@socket = TCPsocket.new(server, port)
end
def to_server(text)
@socket.gets(text)
return true
end
def from_server()
return @socket.puts
end
end
irc = Irc.new
irc.connect(“irc.homelien.no”, 6667)
irc.to_server(‘NICK: testbot’)
irc.to_server(‘USER: testbot “” “irc.efnet.net” :testbot’)
go = true
while go == true
if (text = irc.from_server)
puts(text)
end
end

Doesn’t work though, I just get the loop.

def to_server(text)
@socket.gets(text)
return true
end
def from_server()
return @socket.puts
end

You’ve got gets and puts the wrong way around. Read them as Get String
and Put String, gets reads from the socket and puts writes to it.

Also, there’s a protocol error there, you want to_server to read like
this:

def to_server(text)
@socket.puts(text.to_s+"\r\n") # The line breaks are important, must
be CRLF.
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs