I have two classes: Item and ItemCollection. Item has several attributes: attr1, attr2, attr3, etc., and ItemCollection holds an array of Item instances, along with some manipulating methods.
Question 1: is this the appropriate way to handle a collection of objects?
I want a method: ItemCollection#itemExists(needleItem), which returns true if needleItem.attr1 == attr1 for some item within the array of the ItemCollection instance.
Question 2: What is the best way to do this?
Looks like you could do count. http://ruby-doc.org/core-1.9.3/Array.html#method-i-count
simple code for this would be:
I suppose needleitem is in the array ? If so any count greater than 1 for c should be fine. If not, any count greater than 0 for c should satisfy. Plus you get the total count.
-Douglas