4

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?

Len
  • 2,093
  • 4
  • 34
  • 51
  • 1
    Why would you expect `send` to call the `Adapter` function if it is a `DRbObject` ? – oldergod Oct 04 '12 at 07:30
  • 2
    Have mercy, whatever you do, do not override ``send`` method. Or do it if you have a great idea in mind. But if you don't, stay content with defining eg. ``send_something`` and ``receive_something``. – Boris Stitnicky Oct 04 '12 at 07:56
  • @oldergod. The code starting the `Adapter` starts a DRb server so my application can communicate with the the adapter by having a DRbObject connecting to the same uri. like this: http://segment7.net/projects/ruby/drb/introduction.html – Len Oct 04 '12 at 07:56
  • @Boris, You might be right. But it feels ugly, something with programming INTO a language vs IN a language :) I think the safest thing to do in this case is go for renaming to 'send_message' (or 'puts' or 'transmit' whatever) – Len Oct 04 '12 at 07:59
  • @SirLenz0rlot: No, not ``puts``, pleeese. – Boris Stitnicky Oct 04 '12 at 09:42
  • @BorisStitnicky Haha, i meant 'put'. I went for `sent_message` – Len Oct 04 '12 at 09:44

1 Answers1

2

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.

Community
  • 1
  • 1
davogones
  • 7,321
  • 31
  • 36
  • I'll mark this answer as accepted answer because this probably was the answer I was looking at at the time. I however, choose to rename it, as Boris suggested – Len Apr 25 '13 at 07:56