En bref. Un proc et une lambda sont le même objet, Proc. Ils diffèrent par deux règles : la lambda est stricte sur les arguments, et son return ne sort que d’elle-même.
Le modèle en une phrase
Le proc est un fragment de code ; la lambda est une petite fonction. Même objet, deux politiques.
In short. A proc and a lambda are the same object, Proc. They differ by two rules: a lambda is strict about arguments, and its return only leaves itself.
The model in one sentence
A proc is a code fragment; a lambda is a small function. Same object, two policies.
pr = proc { |a, b| [a, b] }pr.call(1) # => [1, nil]proc { return 1 }.call
pr = proc { |a, b| [a, b] }— On crée unProcà deux paramètres.pr.lambda?vautfalse.We create aProcwith two parameters.pr.lambda?isfalse.pr.call(1) # => [1, nil]— Un seul argument donné : le proc est souple,bvautnil. Aucune erreur.Only one argument given: the proc is lenient,bisnil. No error.proc { return 1 }.call—returndans un proc quitte la méthode qui l'entoure : la ligne suivante n'est jamais atteinte.returninside a proc exits the enclosing method: the next line is never reached.
La preuve, avec Ruby 3.4
pr = proc { |a, b| [a, b] }
la = lambda { |a, b| [a, b] }
p pr.call(1) # => [1, nil]
p pr.call(1, 2, 3) # => [1, 2]
p pr.call([4, 5]) # => [4, 5]
p pr.lambda?, la.lambda? # => false, trueLe proc s’arrange avec ce qu’on lui donne : il complète avec nil, ignore l’excédent et déballe un tableau.
L’erreur, telle que Ruby l’écrit
la.call(1)wrong number of arguments (given 1, expected 2) (ArgumentError)La lambda se comporte comme une méthode : le bon nombre d’arguments, sinon ArgumentError.
Le piège : return
def t_proc
pr = proc { return 1 }
pr.call
2
end
def t_lam
l = -> { return 1 }
l.call
2
end
p t_proc # => 1
p t_lam # => 2Le return du proc quitte t_proc : le 2 n’est jamais atteint. Celui de la lambda revient juste à l.call, et la méthode continue.
Ce que l’image cache
Les blocs passés avec & se comportent comme des procs, pas comme des lambdas. Et lambda? ne dit rien de la portée : proc comme lambda capturent les variables autour d’eux, c’est la fermeture, objet d’un autre numéro.
Pour aller plus loin
Le numéro ci-dessous compare les deux dans le détail (arité, return, curry, conversion en bloc) avec des exercices.
The proof, with Ruby 3.4
pr = proc { |a, b| [a, b] }
la = lambda { |a, b| [a, b] }
p pr.call(1) # => [1, nil]
p pr.call(1, 2, 3) # => [1, 2]
p pr.call([4, 5]) # => [4, 5]
p pr.lambda?, la.lambda? # => false, trueThe proc makes do with what it gets: it pads with nil, ignores the excess and unpacks an array.
The error, as Ruby writes it
la.call(1)wrong number of arguments (given 1, expected 2) (ArgumentError)The lambda behaves like a method: the right number of arguments, or ArgumentError.
The trap: return
def t_proc
pr = proc { return 1 }
pr.call
2
end
def t_lam
l = -> { return 1 }
l.call
2
end
p t_proc # => 1
p t_lam # => 2The proc’s return exits t_proc: the 2 is never reached. The lambda’s only goes back to l.call, and the method carries on.
What the picture hides
Blocks passed with & behave like procs, not lambdas. And lambda? says nothing about scope: both procs and lambdas capture the variables around them, that’s the closure, the subject of another issue.
Go deeper
The issue below compares the two in detail (arity, return, curry, converting to a block) with exercises.