From: Bernhard Brodowsky Date: 2012-09-04T21:34:34+09:00 Subject: Socket Decorator Hi, I am implementing a special kind of socket that takes an io object (usually a socket) as its argument and then behaves like a socket, but does some transformations before/after it sends/receives, so in pseudo code, it could look about like this: def send(text) transform(text) @io.send(text) end def receive text = @io.gets inverse_transform(text) end But now I would like to implement the io interface like all those other io classes, but it seems a lot of work to do it "manually" because there are so many similar methods. Simply "def_delegators" is also not sufficient because I have to do some transformations first, some simple method_missing tricks are also bad because the different methods take different arguments, sometimes and furthermore, I don't really like this trick except if the generated methods really depend on the context (like in ActiveRecord) because for example, in irb, a.methods doesn't return anything useful. Since the length of the messages I send through the internal sockets are fixed (because of the kind of transformations I do), I should probably use an internal buffer and for example return the next byte of that buffer if getc is called and if the buffer is empty, receive the next message and put it into the buffer. But what should I use for the buffer? I could use a string, but I guess +=ing and splitting strings all the time may not be the most efficient way to do it. Does anyone see a good solution to my problem, how I could implement this in an elegant way with some meta programming trick or so without having to implement all the gets puts send recv etc myself? -- Posted via http://www.ruby-forum.com/.