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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:21:45+00:00 2026-06-09T15:21:45+00:00

Ruby noob here! I have an array of structs that look like this Token

  • 0

Ruby noob here!

I have an array of structs that look like this

Token = Struct.new(:token, :ordinal)

So an array of these would look like this, in tabular form:

 Token | Ordinal
 ---------------
    C  | 2
    CC | 3
    C  | 5

And I want to group by the “token” (i.e. the left hand column) of the struct and get a count, but also preserve the “ordinal” element. So the above would look like this

 Token | Merged Ordinal | Count
 ------------------------------
    C  | 2, 5           | 2
    CC | 3              | 1

Notice that the last column is a count of the grouped tokens and the middle column merges the “ordinal”. The first column (“Token”) can contain a variable number of characters, and I want to group on these.

I have tried various methods, using group_by (I can get the count, but not the middle column), inject, iterating (does not seem very functional) but I just can’t get it right, partly because I don’t have a good grasp of Ruby and the available operations / functions.

I have also had a good look around SO, but I am not getting very far.

Any help, pointers would be much appreciated!

  • 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-09T15:21:47+00:00Added an answer on June 9, 2026 at 3:21 pm

    Use Enumerable#group_by to do the grouping for you and use the resulting hash to get what you want with map or similar.

    structs.group_by(&:token).map do |token, with_same_token|
      [token, with_same_token.map(&:ordinal), with_same_token.size]
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ruby Motion comes with a lot of pre-built functions that are formatted like this:
I am new to Ruby and Rails so sorry if this looks too noob.
Noob question for Ruby on Rails- here's an example of my situation: If I
Ruby has a select method that takes an array and returns a subarray consisting
Ruby doesn't seem to have a facility for defining a protected/private block like so:
First of all you have to know I'm total Ruby noob :) I installed
This seems like a noob question, but the simple answer is eluding me. I
This is basically a very noob question about Ruby console (IRB), but I couldn't
This is probably very straight forward and simple, but I'm very new and noob
I am a NOOB to Rails 3 and just completed the Ruby-on-Rails tutorial. This

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.