From: Jean-Hugues ROBERT Date: 2002-05-24T01:50:21+09:00 Subject: Re: Overriding TCPServer.accept Hello, At 00:17 23/05/2002 +0900, you wrote: >Hi all, > > >If I have a class SIPSocket that inherits from TCPSocket and SIPServer >that inherits from TCPServer. I'd like SIPServer.accept to return a >SIPSocket instead of a TCPSocket. Unfortunately TCPServer and TCPSocket >are binary object libs and so I have to do something like this: > >class SIPServer < TCPServer >... > def accept > SIPSocket.create(super) > end >... >end > >where SIPSocket.create adds singleton methods to the TCPSocket returned >by super and returns it, making it into a "pseudo" SIPSocket. It has >all the methods and data of SIPSocket after accept is called but the >object type is still a TCPSocket. I'd like to get rid of this as it >means I'd have two types of SIPSockets in my program. "True" SIPSockets >produced by SIPSocket.new and "pseduo" SIPSockets (TCPSockets in >disguise) returned by SIPServer.accept. Any ideas on how to get round >this, preferably without writing C code (my C skills are not existent at >the moment)? It would be nice to specify that TCPServer returned >SIPSocket instead, as SIPSocket already has all TCPSockets methods, but >that might be a different thread entirely. > >Thanks in advance, >Farrel Lifson I don't know of any other solution as of today. There is probably a need for an obj.extend( a_class, become_flag) that would change the class of obj in addition to injecting methods. Alternatively, a smalltalk like obj.become( a_class) would help too. In the mean time, you may want to have your pseudo SIPSocket look even more like a genuine SIPSocket. For example you may redefine .kind_of? or .type(). That is a hack and it is probably more complex than it first look (any candidate to write a generic Object.become() this way ?) Another solution is to avoid the problem altogether. I.E. make it so that your code does not explicitly check the class of the objects. Often xx.kind_of? yy can be changed into xx.respond_to? :a_method. This result in more generic code (look at the .to_str() case, "any object that implement .to_str() should be treated as a string", this is a Ruby idiom I think). Yours, Jean-Hugues ------------------------------------------------------------------------- Web: http://hdl.handle.net/1030.37/1.1 Phone: +33 (0) 4 92 27 74 17