Search code examples
rubyoverridingsenddrb

Ruby DRbObject and overriding send


I had a funny situation today (simplified):

Ive a communication object of type Adapter, with a #send and a #receive method. My application communicates with it by DRb. So in my application, I have a DRbObject, lets say foo.

Now, calling

foo.send(msg, dest)

calls the #send on DRbObject, instead of Adapter.

Most easy fix would be of course to rename the send method. But, I'd like to stay as close to my documentation/underlying API as possible.

What do you think? Should I rename the send method, or is there a neat (local) hack for this?


Solution

  • DRbObject does it's remote message routing magic with #method_missing. So, clearly, we should undefine the #send method from foo so it will delegate to #method_missing instead!

    foo.singleton_class.class_eval { undef_method :send }

    Or to do it in a more object-oriented way:

    AdapterDRbObject < DRbObject
      undef_method :send
    end
    

    As for whether you should do this, that's up for debate.

    It's actually "okay" to override/remove #send, because everyone is supposed to know that you should always call #__send__ instead. (In fact, if you look at DRbObject#method_missing, it calls #__send__.)

    On the other hand, #send is a pretty core concept of Ruby and might confuse future maintainers of the code.