Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby Modules Store Inventory Using Modules Creating an In Stock Report

Daniel Lieberman
Daniel Lieberman
7,813 Points

Why do we need to extend Enumerable from the Inventoryable module in the store project?

I am working through the store project for Ruby and in the "Creating an In Stock Report" video, Jason mentioned that we needed to call klass.extend(Enumerable) from the self.included method in the Inventoryable module in order for us to be able to call select on the instances. He seems to have just kind of mentioned it in passing like it was clear why that was the case. I don't understand why we needed to do that, though? We never call select on the class itself.

In fact, after commenting out the line klass.extend(Enumerable) and our definition of each but leaving everything the same, the code still worked perfectly! Am I missing something?

So far my only guess is that all of this .select and .each stuff is happening on an array, and the Array class comes with the Enumerable module automatically?

6 Answers

Montalvo Miguelo
Montalvo Miguelo
24,789 Points

We are using each method on an array of instances, so we do not need to Implement from Enumerable (arrays already do)

By other hand if we have to use Enumerable methods on our own classes, then we need to implement Enumerable like Jason Seifer did

Shirt.each {} 
Shirt.find {}
Francois van der Hoven
Francois van der Hoven
2,026 Points

Hi Daniel,

You are right. I have also come to the conclusion that klass.extend(Enumerable) is not required. I have even commented out the each(&block) method and found that it makes no difference. However, I have also used the byebug gem to trace through the code and I have found the the Enumerable module is defined inside Shirt and Pant. I do not know why but this is why your code is still working.

Regards, Francois

I did this exact same thing, commenting out all of the enumerable stuff and had this exact same question, if Jason Siefer has any answers, that would be awesome.



Kang-Kyu Lee
Kang-Kyu Lee
52,045 Points

You can do self.select instead of instances.select with the klass.extend(Enumerable) line. Interesting thing is, it is not klass.include but klass.extend.

Bembemcha Pebam
Bembemcha Pebam
2,338 Points

its a shame that, there is no response to this question so far from the Treehouse folks

Donghai Zhang
Donghai Zhang
Front End Web Development Techdegree Student 7,036 Points

I do agree that there is no need to extend the Enumerable module to achieve this project. The instances itself is an array, which apparently has built-in Enumerable module already. Why bother extend it in anyway?