When Enumerable is included, the class must provide “each” method.
1 min readAug 8, 2021
We have to be careful not to forget it.
When I was writing codes like below referring to a book, I noticed the fact that each method is not used but included.
class Comics extend Forwardable def_delegators :@comics, :size, :each include Enumerable
def initialize(comics) @comics = comics end def publishings @comics.select{ |comic| comic.published } endend
If each is not included, the codes output an error like this in publishing method.
undefined method `each' for #<Comics:0x00007fde00873030> (NoMethodError)
That was confusing because each method was not defined in Comics class.
However, checking out ruby-doc helped me to understand the cause.
“The class must provide a method each, which yields successive members of the collection.”