Why does Ruby use respond_to? instead of responds_to?

12,554

Solution 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

Solution 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.

Share:
12,554

Related videos on Youtube

Nick
Author by

Nick

Updated on April 14, 2020

Comments

  • Nick
    Nick about 4 years

    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.

    • julx
      julx about 13 years
      Maybe it's trying to be imperative: hey you object, RESPOND TO!
    • oligan
      oligan about 13 years
      I can't believe this question hasn't been asked before!
  • sawa
    sawa about 13 years
    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.
  • oligan
    oligan about 11 years
    @sawa like the "dictionary form" (if that's the correct term for 辞書形) of Japanese verbs?
  • sawa
    sawa about 11 years
    Yes. But Japanese does not have person-number agreement, so there is no difference in Japanese. It is usually called 終止形 (the ending form).
  • Nate
    Nate over 10 years
    Why not both respond_to? and responds_to?? Ruby has a bunch of methods that are aliased like this.
  • nroose
    nroose almost 9 years
    I guess I always think of the receiver as 'it'.
  • sawa
    sawa almost 9 years
    @Nate Why not responding_to?, responded_to? in addition to that?
  • Nate
    Nate almost 9 years
    @Sawa, to me those kind of sound more like event handler names.
  • Zack Xu
    Zack Xu almost 9 years
    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?
  • Christopher Swasey
    Christopher Swasey over 8 years
    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.
  • Ninjaxor
    Ninjaxor over 8 years
    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 :)
  • Devon Parsons
    Devon Parsons about 8 years
    @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
  • Gambai
    Gambai about 7 years
    you.responds_to? makes more sense anyway. ruby got this one incorrect.