Ruby send vs __send__

后端 未结 5 1504
长情又很酷
长情又很酷 2020-12-12 13:36

I understand the concept of some_instance.send but I\'m trying to figure out why you can call this both ways. The Ruby Koans imply that there is some reason bey

5条回答
  •  渐次进展
    2020-12-12 14:18

    Some classes (for example the standard library's socket class) define their own send method which has nothing to do with Object#send. So if you want to work with objects of any class, you need to use __send__ to be on the safe side.

    Now that leaves the question, why there is send and not just __send__. If there were only __send__ the name send could be used by other classes without any confusion. The reason for that is that send existed first and only later it was realized that the name send might also usefully be used in other contexts, so __send__ was added (that's the same thing that happened with id and object_id by the way).

提交回复
热议问题