45

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.

Nick
  • 8,483
  • 10
  • 46
  • 65

2 Answers2

76

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
Matt
  • 14,353
  • 5
  • 53
  • 65
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
  • 1
    I believe it's not that Matz prefers second person singular or third person plural, but he prefers the default (to-less infinitive) form of a verb. – sawa Mar 12 '11 at 06:06
  • @sawa like the "dictionary form" (if that's the correct term for 辞書形) of Japanese verbs? – Andrew Grimm Apr 11 '13 at 03:32
  • Yes. But Japanese does not have person-number agreement, so there is no difference in Japanese. It is usually called 終止形 (the ending form). – sawa Apr 11 '13 at 03:38
  • 2
    Why not both `respond_to?` and `responds_to?`? Ruby has a bunch of methods that are aliased like this. – Nate Feb 07 '14 at 02:19
  • @Nate Why not `responding_to?`, `responded_to?` in addition to that? – sawa Jun 20 '15 at 02:47
  • @Sawa, to me those kind of sound more like event handler names. – Nate Jun 21 '15 at 22:34
  • There's no guarantee the subject preceding `respond_to` is singular. It could a collection (eg `Array`, `Set`). So trying to achieve grammatical agreement is pointless. `respond_to` is shorter than `responds_to`. So why get into the verb conjugation business, which is only a feature of certain natural languages? – Zack Xu Jul 31 '15 at 14:18
  • 2
    I'm having trouble coming up with a construction in English where `responds_to?` actually makes sense and is proper. Grammatically correct: "Does it `respond_to?` :bar"; "Does foo `respond_to` :bar?". Only by casting the indicative "Foo `responds_to` :bar" to an ersatz interrogative—"Foo `responds_to` :bar?" does it make any sense, and that is definitely taking liberties with the language. Long story short—`respond_to?` would be correct even if being English-like were the primary consideration. – Christopher Swasey Oct 10 '15 at 20:48
  • I always think in my head "[wait!] foo responds_to :bar???" that's well formed spoken English, but only if you intonate the phrase in the form of a question. It's helpful remembering that it's shorter without the 's' and so maybe I will not have to google this again :) – Ninjaxor Nov 17 '15 at 02:55
  • 3
    @ChristopherSwasey The most common use I have for the method looks like this: `if my_object.respond_to?(:with_indifferent_access) ... else ... end`. Clearly `responds_to` is more grammatically correct here. However I understand Matz's logic – Devon Parsons May 25 '16 at 14:38
  • you.responds_to? makes more sense anyway. ruby got this one incorrect. – Gambai Mar 22 '17 at 21:02
8

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.

sawa
  • 165,429
  • 45
  • 277
  • 381