Often times when writing a ruby based application (yes that includes Rails) you end up with a bunch of objects that you would like to treat like an array but with special powers (okay methods). This usually comes up when you want to start asking questions about your array.
Example Scenario: let’s say you have a list of animal objects and you want to know how many of them have 4 legs. In your code you could iterate through the array every time and check for legs and then tally them up. Or you could have an AnimalCollection object and simply ask the collection how many have four legs and receive a proper integer. Now that we have a use case, here is an example how I would generally do this sort of thing by using the enumerable mixin.
It’s worth noting that you could subclass your AnimalCollection Class from Array. However I would consider this an antipattern as the mixin offers a clean interface for making a collection without altering the the objects class hierarchy.
If you agree disagree or otherwise found this article useful, then please feel free to leave a comment and i’ll be sure to get back to you.