When Enumerable is included, the class must provide “each” method.

Tomoharu Tsutsumi
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.”

My LinkedIn account is below! Please contact me!

https://www.linkedin.com/in/tomoharu-tsutsumi-56051a126/

--

--

Tomoharu Tsutsumi

Senior Software Engineer at two industry-leading startups ( Go | Ruby | TypeScript | JavaScript | Gin | Echo | Rails | React | Redux | Next)