Why does Ruby use respond_to? instead of responds_to?
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.
Related videos on Youtube
Nick
Updated on April 14, 2020Comments
-
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 ofresponds_to?
It always seems awkward to me but maybe that's because I'm used to
respondsToSelector
in objective-c.-
julx about 13 yearsMaybe it's trying to be imperative: hey you object, RESPOND TO!
-
oligan about 13 yearsI can't believe this question hasn't been asked before!
-
-
sawa about 13 yearsI 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 about 11 years@sawa like the "dictionary form" (if that's the correct term for 辞書形) of Japanese verbs?
-
sawa about 11 yearsYes. But Japanese does not have person-number agreement, so there is no difference in Japanese. It is usually called 終止形 (the ending form).
-
Nate over 10 yearsWhy not both
respond_to?
andresponds_to?
? Ruby has a bunch of methods that are aliased like this. -
nroose almost 9 yearsI guess I always think of the receiver as 'it'.
-
sawa almost 9 years@Nate Why not
responding_to?
,responded_to?
in addition to that? -
Nate almost 9 years@Sawa, to me those kind of sound more like event handler names.
-
Zack Xu almost 9 yearsThere's no guarantee the subject preceding
respond_to
is singular. It could a collection (egArray
,Set
). So trying to achieve grammatical agreement is pointless.respond_to
is shorter thanresponds_to
. So why get into the verb conjugation business, which is only a feature of certain natural languages? -
Christopher Swasey over 8 yearsI'm having trouble coming up with a construction in English where
responds_to?
actually makes sense and is proper. Grammatically correct: "Does itrespond_to?
:bar"; "Does foorespond_to
:bar?". Only by casting the indicative "Fooresponds_to
:bar" to an ersatz interrogative—"Fooresponds_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 over 8 yearsI 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 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
. Clearlyresponds_to
is more grammatically correct here. However I understand Matz's logic -
Gambai about 7 yearsyou.responds_to? makes more sense anyway. ruby got this one incorrect.