En bref. Une méthode n’est pas une valeur qu’on transporte. Pour en avoir une, on la demande : on obtient un objet Method, qui fige l’implémentation du moment.
Le modèle en une phrase
Un objet
Methodest un instantané. Envoyer le message ré-résout à chaque appel.
In short. A method isn’t a value you carry around. To get one you ask for it: you get a Method object, which freezes the implementation of that moment.
The model in one sentence
A
Methodobject is a snapshot. Sending the message re-resolves on every call.
m = c.method(:miaule)class Chat; def miaule = "MIAOU"; endm.callc.miaule # ou c.send(:miaule)
m = c.method(:miaule)—c.method(:miaule)demande la méthode : on obtient un objetMethodqui garde l'implémentation du moment.c.method(:miaule)asks for the method: you get aMethodobject holding the implementation of that moment.class Chat; def miaule = "MIAOU"; end— On redéfinit la méthode dans la classe. Le handle, lui, n'a pas bougé.We redefine the method in the class. The handle hasn't moved.m.call— Le handle rend l'ancienne version : c'est un instantané.The handle returns the old version: it's a snapshot.c.miaule # ou c.send(:miaule)— Envoyer le message ré-résout à chaque appel : on obtient la version actuelle.Sending the message re-resolves on every call: you get the current version.
La preuve, avec Ruby 3.4
class Chat; def miaule = "miaou"; end
c = Chat.new
m = c.method(:miaule)
class Chat; def miaule = "MIAOU"; end
p m.call # => "miaou"
p c.miaule # => "MIAOU"
p c.send(:miaule) # => "MIAOU"Le handle n’a pas suivi la redéfinition, le message oui.
Passer une méthode comme un bloc
p [1, 2, 3].map(&10.method(:+)) # => [11, 12, 13]Détacher la méthode
um = Chat.instance_method(:miaule)
p um.class # => UnboundMethod
p um.bind(Chat.new).call # => "MIAOU"L’erreur, telle que Ruby l’écrit
c.method(:vole)
undefined method 'vole' for class 'Chat' (NameError)Ce que l’image cache
Un handle capturé tôt peut garder un comportement périmé sans qu’aucune erreur ne le signale. Si tu veux « la version actuelle », envoie le message. Si tu veux une version précise, garde le Method, en sachant ce qu’il fige.
Pour aller plus loin
Le numéro ci-dessous détaille l’objet Method, l’instantané et le message, avec des exercices.
The proof, with Ruby 3.4
class Chat; def miaule = "miaou"; end
c = Chat.new
m = c.method(:miaule)
class Chat; def miaule = "MIAOU"; end
p m.call # => "miaou"
p c.miaule # => "MIAOU"
p c.send(:miaule) # => "MIAOU"The handle didn’t follow the redefinition, the message did.
Passing a method as a block
p [1, 2, 3].map(&10.method(:+)) # => [11, 12, 13]Detaching the method
um = Chat.instance_method(:miaule)
p um.class # => UnboundMethod
p um.bind(Chat.new).call # => "MIAOU"The error, as Ruby writes it
c.method(:vole)
undefined method 'vole' for class 'Chat' (NameError)What the picture hides
A handle captured early can keep a stale behaviour without any error flagging it. If you want “the current version”, send the message. If you want a precise version, keep the Method, knowing what it freezes.
Go deeper
The issue below details the Method object, the snapshot and the message, with exercises.