En bref. En Ruby, une méthode qui appelle x.parler marche avec n’importe quel objet qui répond à parler. Pas besoin d’interface, ni de classe commune : le message est le contrat.
Le modèle en une phrase
On ne demande pas « quel type es-tu ? » mais « réponds-tu à ce message ? ».
In short. In Ruby, a method that calls x.parler works with any object that answers parler. No interface and no shared class: the message is the contract.
The model in one sentence
We don’t ask “what type are you?” but “do you answer this message?”.
faire_parler(Canard.new)x.parler
faire_parler(Canard.new)— La méthode reçoit unCanard. Elle ne vérifie ni sa classe ni son type.The method receives aCanard. It checks neither its class nor its type.x.parler— Le messageparlerest envoyé, l'objet répond"coin". Aucun lien de parenté n'était requis.The messageparleris sent, the object answers"coin". No family tie was required.
La preuve, avec Ruby 3.4
class Canard; def parler = "coin"; end
class Robot; def parler = "bip"; end
def faire_parler(x) = x.parler
p faire_parler(Canard.new) # => "coin"
p faire_parler(Robot.new) # => "bip"Canard et Robot ne partagent rien, ni superclasse ni module. La méthode s’en moque.
L’erreur, telle que Ruby l’écrit
class Pierre; end
faire_parler(Pierre.new)undefined method 'parler' for an instance of Pierre (NoMethodError)Tester avant d’envoyer
def parler_si_possible(x) = x.respond_to?(:parler) ? x.parler : "muet"
p parler_si_possible(Pierre.new) # => "muet"
p Canard.new.respond_to?(:parler) # => truerespond_to? pose la question du comportement, pas celle de la classe.
Ce que l’image cache
Le dessin ne montre qu’une méthode. En vrai, un objet peut répondre à un message en apparence sans le définir (method_missing), ou le définir sans faire ce qu’on attend : répondre ne garantit pas le bon sens. C’est le sujet des numéros suivants sur les protocoles implicites et sur le moment où le canard trahit.
Pour aller plus loin
Le numéro ci-dessous détaille le duck typing avec des exercices.
The proof, with Ruby 3.4
class Canard; def parler = "coin"; end
class Robot; def parler = "bip"; end
def faire_parler(x) = x.parler
p faire_parler(Canard.new) # => "coin"
p faire_parler(Robot.new) # => "bip"Canard and Robot share nothing, no superclass and no module. The method doesn’t care.
The error, as Ruby writes it
class Pierre; end
faire_parler(Pierre.new)undefined method 'parler' for an instance of Pierre (NoMethodError)Check before sending
def parler_si_possible(x) = x.respond_to?(:parler) ? x.parler : "muet"
p parler_si_possible(Pierre.new) # => "muet"
p Canard.new.respond_to?(:parler) # => truerespond_to? asks about behavior, not about the class.
What the picture hides
The drawing shows one method. In reality an object can seem to answer a message without defining it (method_missing), or define it without doing what you expect: answering doesn’t guarantee sense. That’s the subject of the next issues on implicit protocols and on when the duck betrays you.
Go deeper
The issue below details duck typing with exercises.