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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:40:37+00:00 2026-06-04T17:40:37+00:00

I have an array in Ruby that consists of 5 empty arrays. I am

  • 0

I have an array in Ruby that consists of 5 empty arrays. I am trying to use the << operator to push a string into the first array, but the result is that the string gets pushed into ALL of the arrays. Please help me understand this.

The expected output is:

# => [["car"], [], [], [], []]

but instead I get:

# => [["car"], ["car"], ["car"], ["car"], ["car"]]

irb dump:

1.9.3-p194 :001 > output = Array.new(5, [])
 => [[], [], [], [], []] 
1.9.3-p194 :002 > output.inspect
 => "[[], [], [], [], []]" 
1.9.3-p194 :003 > output[0].inspect
 => "[]" 
1.9.3-p194 :004 > output[0] << "car"
 => ["car"] 
1.9.3-p194 :005 > output.inspect
 => "[[\"car\"], [\"car\"], [\"car\"], [\"car\"], [\"car\"]]" 
  • 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-04T17:40:38+00:00Added an answer on June 4, 2026 at 5:40 pm

    They’re all the same object:

    ree-1.8.7-2012.02 :001 > output = Array.new(5, [])
     => [[], [], [], [], []] 
    ree-1.8.7-2012.02 :002 > output[0]
     => [] 
    ree-1.8.7-2012.02 :003 > output[0].object_id
     => 2219989240 
    ree-1.8.7-2012.02 :004 > output[1].object_id
     => 2219989240 
    ree-1.8.7-2012.02 :005 > output[2].object_id
     => 2219989240 
    ree-1.8.7-2012.02 :006 > output[3].object_id
     => 2219989240 
    ree-1.8.7-2012.02 :007 > output[4].object_id
     => 2219989240 
    ree-1.8.7-2012.02 :008 > 
    

    Try this:

    ree-1.8.7-2012.02 :008 > output = []
     => [] 
    ree-1.8.7-2012.02 :009 > 5.times{output << []}
     => 5 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have an array of arrays in Ruby, [[100,300], [400,500]] that I'm building
I have a Ruby method that searches an array of hashes and returns a
I have a ruby array that looks something like this: my_array = ['mushroom', 'beef',
I have a Ruby array containing some string values. I need to: Find all
I have a 2-dimensional array in Ruby that I want to produce a working
I have an Array of Arrays that contains numbers in a particular order. I
I have an array of ruby objects that looks something like this: [#<email: someemail
I have an array of latitude/longitude coordinate pairs that represent a polygon. I'm trying
I have an array of hashes in ruby that I render with render :json
I have a string that I am trying to work with in using the

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.