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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:00:17+00:00 2026-05-31T22:00:17+00:00

a = Array.new(3,[]) a[1][0] = 5 a => [[5], [5], [5]] I thought this

  • 0
a = Array.new(3,[])
a[1][0] = 5
a => [[5], [5], [5]]

I thought this doesn’t make sense!
isn’t it should a => [[], [5], []]
or this’s sort of Ruby’s feature ?

  • 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-31T22:00:18+00:00Added an answer on May 31, 2026 at 10:00 pm

    Use this instead:

    a = Array.new(3){ [] }
    

    With your code the same object is used for the value of each entry; once you mutate one of the references you see all others change. With the above you instead invoke the block each time a new value is needed, which returns a new array each time.


    This is similar in nature to the new user question about why the following does not work as expected:

    str.gsub /(<([a-z]+)>/, "-->#{$1}<--"
    

    In the above, string interpolation occurs before the gsub method is ever called, so it cannot use the then-current value of $1 in your string. Similarly, in your question you create an object and pass it to Array.new before Ruby starts creating array slots. Yes, the runtime could call dup on the item by default…but that would be potentially disastrous and slow. Hence you get the block form to determine on your own how to create the initial values.

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

Sidebar

Related Questions

@search_results = Array.new duplicates = Set.new results.each { |result| @search_results.push(result) unless duplicates.add?(result[:url]) } This
I have this block of code: users = Array.new users << User.find(:all, :conditions =>
This code works fine: $result = $client->__call(optionalInfo, array( new SoapParam(..., client), new SoapParam(..., add_code))
ruby 1.8.7 (2008-08-11 patchlevel 72) [i586-linux] From script/console: >> pairs = Array.new(2).map!{Array.new(2).map!{Array.new(2, Array.new)}} =>
with a new array I do this: $aVal = array(); $aVal[key1][var1] = something; $aVal[key1][var2]
Sometimes JavaScript doesn't make sense to me, consider the following code that generates a
I have read through this post over and over, and yet it doesn't make
Element[] array = {new Element(1), new Element(2), new Element(3)}; How do I convert the
You have: val array = new Array[Array[Cell]](height, width) How do you initialize all elements
var grossBrackets = new Array( '300', '400', '500', '600', '700', '800', '900', '1000' );

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.