Skip to content

Instantly share code, notes, and snippets.

@jakenotjacob
Last active August 29, 2015 14:11
Show Gist options
  • Save jakenotjacob/3e878d7316c10b3104d1 to your computer and use it in GitHub Desktop.
Save jakenotjacob/3e878d7316c10b3104d1 to your computer and use it in GitHub Desktop.
Rescue Socket error
[14] pry(main)> A.new 'irc.freenode.net', 6667
=> #<A:0x000000034889a8 @sock=#<TCPSocket:fd 8>>
[20] pry(main)> A.new 'localhost', 1233
Errno::ECONNREFUSED: Connection refused - connect(2) for "localhost" port 1233
from (pry):6:in `initialize'
[27] pry(main)> A.new 'localhostasdf', 1233
Invalid host or port!
=> #<A:0x00000003263c18>
class A
attr_accessor :sock
def initialize(host, port)
@host = host
@port = port
begin
@sock = TCPSocket.new host, port
rescue SocketError
puts "Invalid host or port!"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment