Why does Ruby use respond_to? instead of responds_to?

两盒软妹~` 提交于 2019-12-04 14:58:36

问题


I'm curious why Ruby's introspection related method to check if an object responds to a method is respond_to? instead of responds_to?

It always seems awkward to me but maybe that's because I'm used to respondsToSelector in objective-c.


回答1:


Matz prefers second person singular or third person plural:

"responds_to?" probably makes more sense to English speakers than "respond_to?".

Maybe. But I'm Japanese. Ruby is not English. It's the basic naming rule to avoid third person singular form in the standard libraries.

  you = Human.new
  if you.respond_to?(:knock)
    ...
  end



回答2:


How do you know that the receiver is always third person singular? It is possible that the receiver be I, we, you, or they, or some other thing that represents plurality. In that case, will you still say that responds_to? is more natural than respond_to?? In order to preserve generality, it is better to name a method in a form as general as possible. Rather than naming a method in third person singular, it makes more sense to name it in the default, to-less infinitive form, which is also used in dictionaries.



来源:https://stackoverflow.com/questions/5280556/why-does-ruby-use-respond-to-instead-of-responds-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!