I’m writing a Ruby class that extends TCPSocket. Assume it looks something like this:
class FooSocket < TCPSocket
def hello
puts 'hello'
end
end
I have a TCPServer listening for incoming connections
server = TCPServer.new 1234
socket = server.accept
When my server finally accepts a connection, it will return a TCPSocket. However, I want a FooSocket so that I can call socket.hello.
How can I change TCPSocket into a FooSocket?
I could duck-punch the methods and attributes I want directly onto the TCPSocket class, but I’m using it elsewhere and so I don’t want to do that.
Probably the easiest solution is to write a class that encapsulates a TCPSocket, and just pass the socket returned by accept as a param. However, I’m interested to know how to do it through inheritance—I’ve been trying to bend my mind around it but can’t figure it out.
Thanks in advance.
I think that the easiest way to do that would be to make
FooSocketa module, and thenextendit into yourTCPSockets that need the special methods. Here are some examples of how this could be used:You could also easily override
TCPServerto return one of these:If you didn’t want to override standard classes, you could define a singleton class on your server (which is what I would do) because you will most likely only have one server. To do this, replace the line
class TCPServerin the above example withclass << server.