Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Ruby use respond_to? instead of responds_to?

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.

like image 328
Nick Avatar asked Mar 12 '11 04:03

Nick


People also ask

What is Respond_to in Ruby?

respond_to? is a Ruby method for detecting whether the class has a particular method on it. For example, @user.respond_to?('eat_food')


1 Answers

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 
like image 138
Andrew Grimm Avatar answered Sep 21 '22 18:09

Andrew Grimm