TCPSocket subclass & TCPServer.accept

G

gabriele renzi

Hi gurus and nubys,

The problem I'm facing is:
I wanto to use a subclass of TCPSocket.
Everything works fine while I'm using it to connect outside.

But now I'd like to use a MySocket even when I'm receiving
connections.
BTW, TCPServer.accept just gives me a classic vanilla TCPSocket.

now I have 3 way to go:
-add methods to TCPSocket instead of subclass it
-add a MyServer class as a subclass of TCPServer
-wriap up a TCPSocket in a MySocket class instead of subclassing it.

My point is: this ways are ugly to me.

So, is there a way in ruby to get a subclass instance from a
superclass instance?

I know that often this could be impossible, but who knows ;)

something like:
class MyKlass < Klass
...
end

MyKlass.get_instance_from(Klass.new)
 
N

nobu.nokada

Hi,

At Wed, 10 Sep 2003 05:46:37 +0900,
gabriele said:
But now I'd like to use a MySocket even when I'm receiving
connections.
BTW, TCPServer.accept just gives me a classic vanilla TCPSocket.

In 1.7 or later:

class MyServer < TCPServer
def accept
MySocket.for_fd(sysaccept)
end
end

Note that this is not exception safe. It might leak file
descriptors.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top