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 trialDaniel Lieberman
7,813 PointsWhy 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?
6 Answers
Montalvo Miguelo
24,789 PointsWe 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
2,026 PointsHi 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
Hamilton Steele
8,717 PointsI 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.
Thanks,
Hamilton
Kang-Kyu Lee
52,045 PointsYou 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
2,338 Pointsits a shame that, there is no response to this question so far from the Treehouse folks
Donghai Zhang
Front End Web Development Techdegree Student 7,036 PointsI 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?
Hamilton Steele
8,717 PointsHamilton Steele
8,717 PointsSo 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?