I have a class that represents a collection. I included the Enumerable
module into it and defined the method #each
, so that I get all its methods.
But the problem is that Enumerable
's methods don't keep the same class. So, for example, if my class is named Collection
, and if I do Collection#select
, I would like that the result's class is also Collection
(instead of Array
). Is there a way how to achieve this?
Since Enumerable#select
is designed to return an array, you need to tell somewhere how to map that to a Collection
instance. That means, you explicitly need to define Collection#select
. Otherwise Ruby will not know the mapping rule from the original array result of Enumerable#select
to a Collection
instance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With