为什么 Ruby 使用 respond_to?而不是responds_to?
我很好奇为什么 Ruby 用于检查对象是否响应方法的内省相关方法是 respond_to?
而不是 responds_to?
这对我来说总是很尴尬,但也许那是因为我我习惯了 Objective-C 中的 respondsToSelector
。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Matz 更喜欢第二人称单数或第三人称人称复数:
Matz prefers second person singular or third person plural:
你怎么知道接收者总是第三人称单数?接收者可能是
我
、我们
、你
、他们
,或者其他代表的东西复数。那么,您还会说responds_to?
比respond_to?
更自然吗?为了保持通用性,最好以尽可能通用的形式命名方法。与其以第三人称单数命名方法,不如以默认的、to-less 不定式形式命名更有意义,这种形式也在字典中使用。How do you know that the receiver is always third person singular? It is possible that the receiver be
I
,we
,you
, orthey
, or some other thing that represents plurality. In that case, will you still say thatresponds_to?
is more natural thanrespond_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.