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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:56:52+00:00 2026-05-31T00:56:52+00:00

I am trying to learn Ruby, and arrays are giving me some trouble. I

  • 0

I am trying to learn Ruby, and arrays are giving me some trouble.
I have input that I flatten down to the pattern "name, number, name, number". I then want to make an array of 2-element arrays, each containing a name and the next number.

When I push these 2-element arrays into another array the seem to automatically flatten to a 0-dimensional array. What I want is the final array to be of size [N/2][2], N being number of names, or numbers in the input.

http://pastie.org/3542269

The puts with the comment does not happen until all of the elements from the pairs array has been printed, so it looks like this:

Name
1
Name
2
Name
3

When I expected this:

Name
1

Name
2

Name
3

I guess my questions are:

  • How do I put arrays inside an array, to make a jagged one?
  • How do I keep track of how many dimensions my arrays are in Ruby? It’s so much easier when you have to declare a size.
  • 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-31T00:56:53+00:00Added an answer on May 31, 2026 at 12:56 am
    some_array = [[["Name 1", "value 1"], ["Name 2", "value 2"]], [["Name 3", "value 3"], ["Name 4", "value 4"]]]
    
    array = some_array.flatten
    new_array = array.each_slice(2).map do |a, b|
      [a,b]
    end
    #=> [["Name 1", "value 1"],
    #=> ["Name 2", "value 2"],
    #=> ["Name 3", "value 3"],
    #=> ["Name 4", "value 4"]]
    

    which is similar to some_array.flatten(1)

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

Sidebar

Related Questions

so, i'm trying to learn ruby by doing some project euler questions, and i've
Trying to learn about php's arrays today. I have a set of arrays like
I'm trying to learn Ruby, and am going through some of the Project Euler
I'm trying to learn C from a Ruby/PHP/Java background, and I've found that you
I'm trying to learn some basics of Ruby on Rails and encountered a problem
I've recently installed Ruby on Rails 3.2 and have been trying to learn it.
I'm trying to write some ruby that would recursively search a given directory for
I'm trying out some sample problems from a learn Ruby book and I'm getting
I am trying to learn Ruby on Rails, I have followed the instructions from
I have recently begun the process of trying to learn Ruby/Rails and have been

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.