Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9190587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:29:48+00:00 2026-06-17T20:29:48+00:00

I have two classes: Item and ItemCollection . Item has several attributes: attr1 ,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:29:49+00:00Added an answer on June 17, 2026 at 8:29 pm

    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:

    def check_needle
      c = ItemCollection.count { |i|
          needleitem.attr1 == i.attr1 # Each time this value returns true it enumerates
      }
    
      c > 1
      # or 
      if c > 1 then return true end
    end
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes, Item and SoldItem . SoldItem inherits from Item adding some
I have two classes, class A and class B. Class A has a mission
I have a class called Entity, with two child classes: Creature and Item. (I'm
I have two classes: Furniture and Painting . Those are extending Item . Item
I have two classes: Action class, that has a method for executing VBScript files,
I have two classes: public class A{ int money; ArrayList<Item> boughtItems; . . public
I have two classes Item and Category. Items may belong to a single category.
I have two PHP model classes named Category and Item. A Category may have
I have an element that has two classes at all times: <p class=general_description laptop>
i have this two classes: Item<T> : BusinessBase<T> where T : Item<T> { public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.