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 6226301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:59:56+00:00 2026-05-24T08:59:56+00:00

I have got an array of objects in ruby and I want to iterate

  • 0

I have got an array of objects in ruby and I want to iterate through it, ignoring every object, which has a name that already has been processed. For example:

[
  #<Item name: "Item 1", content: "a">,
  #<Item name: "Item 1", content: "b">,
  #<Item name: "Item 2", content: "c">,
  #<Item name: "Item 3", content: "d">,
  #<Item name: "Item 3", content: "e">
]

Should be reduced to

[
  #<Item name: "Item 1">, # Should know that it belongs to content: "a" and "b"
  #<Item name: "Item 2">, # Should know that it belongs to content "c"
  #<Item name: "Item 3">  # Should know that it belongs to content: "d" and "e"
]

A possible (but nasty) solution:

processed = []
items.each do |item|
  next if processed.include?(item.name)
  processed << item.name
  # Output ...

This does not seem very straight forward to me and so I’m looking for an alternative. Another solution would be to store everything in a hash and use the name as an index. But that would require to iterate two times and does also not seem to be the most elegant solution. It would be awesome if anyone had an idea how to iterate elegantly.

Kind Regards, Sebastian

PS: I just realized that all the other items with the same name attribute have to be known to the actually processed item. So my solution won’t even work for that. 🙁

  • 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-05-24T08:59:59+00:00Added an answer on May 24, 2026 at 8:59 am

    Try this:

    array.group_by(&:name).map{|k, v| v.first}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a polymorphic array of objects which implement two (informal) interfaces. I want
This should be so much simpler! I have an array of JSON objects that
In a Flex project, I have an array with objects in it. I want
I have an array of objects that I'd like to filter using a predicate,
I have this NSMutableArray which is a collection of objects that are being moved
Okay, very simple: there is an array containing 3 objects. Each object has a
For a Ruby on Rails app, I'm having this Movie object which has several
Alright, I've got this blank array of objects. I am dynamically finding every node
I have an array of objects. On this object is a NSDecimalNumber property distanceFromDevice.
Okay I have a JSON file and it has a set of objects that

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.