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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:42:15+00:00 2026-05-11T13:42:15+00:00

I have a ruby hash that looks like this { stuff_attributes => { 1

  • 0

I have a ruby hash that looks like this

{ 'stuff_attributes' => {      '1' => {'foo' => 'bar', 'baz' => 'quux'},       '2' => {'foo' => 'bar', 'baz' => 'quux'}     } } 

and I want to turn it into a hash that looks like this

{ 'stuff_attributes' => [     { 'foo' => 'bar', 'baz' => 'quux'},     { 'foo' => 'bar', 'baz' => 'quux'}   ] } 

I also need to preserve the numerical order of the keys, and there is a variable number of keys. The above is super-simplified, but I’ve included a real example at the bottom. What’s the best way to do this?

P.S

It also needs to be recursive

As far as the recursion goes, here’s what we can assume:

1) the key that needs to be manipulated will match /_attributes$/ 2) the hash will have many other keys that do not match /_attributes$/ 3) the keys inside the hash will always be a number 4) an _attributes hash can be at any level of the hash under any other key

this hash is actually the params hash from a create action in the controller. This is a real example of what will need to be parsed with this routine.

{     'commit'=>'Save',      'tdsheet'=>{     'team_id'=>'43',      'title'=>'',      'performing_org_id'=>'10',      'tdsinitneed_attributes'=>{          '0'=>{             'title'=>'',              'need_date'=>'',              'description'=>'',              'expected_providing_organization_id'=>'41'             },          '1'=>{             'title'=>'',              'need_date'=>'',              'description'=>'',              'expected_providing_organization_id'=>'41'             }         },          'level_two_studycollection_id'=>'27',          'plan_attributes'=>{             '0'=>{                 'start_date'=>'', 'end_date'=>''             }         },          'dataitem_attributes'=>{             '0'=>{                 'title'=>'',                  'description'=>'',                  'plan_attributes'=>{                     '0'=>{                         'start_date'=>'',                          'end_date'=>''                         }                     }                 },              '1'=>{                 'title'=>'',                  'description'=>'',                  'plan_attributes'=>{                     '0'=>{                         'start_date'=>'',                          'end_date'=>''                         }                     }                 }             }         },      'action'=>'create',      'studycollection_level'=>'',      'controller'=>'tdsheets' } 
  • 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. 2026-05-11T13:42:16+00:00Added an answer on May 11, 2026 at 1:42 pm

    Note that this might be long to test if all keys are numbers before converting…

    def array_from_hash(h)   return h unless h.is_a? Hash    all_numbers = h.keys.all? { |k| k.to_i.to_s == k }   if all_numbers     h.keys.sort_by{ |k| k.to_i }.map{ |i| array_from_hash(h[i]) }   else     h.each do |k, v|       h[k] = array_from_hash(v)     end   end end 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 138k
  • Answers 139k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First, define a pattern that matches a single point in… May 12, 2026 at 7:38 am
  • Editorial Team
    Editorial Team added an answer This may be faster than using SUB2IND: [r,c] = size(M);… May 12, 2026 at 7:38 am
  • Editorial Team
    Editorial Team added an answer The zipfile module in the standard Python library provides tools… May 12, 2026 at 7:38 am

Related Questions

I have a rails helper in my application_helper.rb file that looks like this: def
I have an array, and I want to make a hash so I can
I'm writing a DSL in Ruby to control an Arduino project I'm working on;
I am looking for template/generator libraries for C++ that are similar to eg. Ruby's

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.