En bref. Le contrat d’Enumerable est minimal : un each qui yielde. Tout le reste en découle.
Le modèle en une phrase
Un seul
each, uninclude Enumerable, et le mixin appelle toneach: il n’a aucune autre source.
In short. The Enumerable contract is minimal: an each that yields. Everything else follows.
The model in one sentence
A single
each, oneinclude Enumerable, and the mixin calls youreach: it has no other source.
def each; yield 3; yield 1; yield 2; endinclude Enumerablet.map { _1 * 2 }t.select(&:odd?) ; t.reduce(:+)
def each; yield 3; yield 1; yield 2; end— Un seuleachqui yielde les éléments. Sansinclude,mapn'existe pas :NoMethodError.A singleeachthat yields the elements. Withoutinclude,mapdoesn't exist:NoMethodError.include Enumerable— Une ligne, et tu hérites d'une soixantaine de méthodes :map,select,reduce,sort…One line, and you inherit around sixty methods:map,select,reduce,sort…t.map { _1 * 2 }—mapn'a pas d'autre source que toneach: il l'appelle, et collecte.maphas no other source than youreach: it calls it, and collects.t.select(&:odd?) ; t.reduce(:+)— Chaque méthode relanceeachdepuis le début : trois appels, trois parcours.Each method restartseachfrom the top: three calls, three passes.
La preuve, avec Ruby 3.4
class Trio
include Enumerable
def each
yield 3; yield 1; yield 2
self
end
end
t = Trio.new
p t.map { _1 * 2 } # => [6, 2, 4]
p t.select(&:odd?) # => [3, 1]
p t.reduce(:+) # => 6
p t.sort # => [1, 2, 3]
p t.min, t.max # => 1, 3L’erreur, telle que Ruby l’écrit
class Sans; def each; yield 1; end; end
Sans.new.map { _1 }
undefined method 'map' for an instance of Sans (NoMethodError)Avoir un each ne suffit pas : c’est include Enumerable qui apporte map.
Chaque méthode relance each
Avec un compteur dans each, après map, select, reduce, sort, min et max, il vaut 6, puis 7 après first, qui s’arrête dès le premier élément.
Ce que l’image cache
Le contrat est minimal, pas gratuit : comme chaque méthode relance each, une structure dont le parcours coûte cher le paiera à chaque appel. Et réécrire map ou select à la main est presque toujours inutile.
Pour aller plus loin
Le numéro ci-dessous détaille le contrat each / Enumerable, avec des exercices.
The proof, with Ruby 3.4
class Trio
include Enumerable
def each
yield 3; yield 1; yield 2
self
end
end
t = Trio.new
p t.map { _1 * 2 } # => [6, 2, 4]
p t.select(&:odd?) # => [3, 1]
p t.reduce(:+) # => 6
p t.sort # => [1, 2, 3]
p t.min, t.max # => 1, 3The error, as Ruby writes it
class Sans; def each; yield 1; end; end
Sans.new.map { _1 }
undefined method 'map' for an instance of Sans (NoMethodError)Having an each isn’t enough: it’s include Enumerable that brings map.
Each method restarts each
With a counter inside each, after map, select, reduce, sort, min and max, it reads 6, then 7 after first, which stops at the first element.
What the picture hides
The contract is minimal, not free: since each method restarts each, a structure whose traversal is expensive pays for it on every call. And rewriting map or select by hand is almost always unnecessary.
Go deeper
The issue below details the each / Enumerable contract, with exercises.