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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:13:02+00:00 2026-06-09T12:13:02+00:00

Why both pieces of code are not printing the same thing. I was intending

  • 0

Why both pieces of code are not printing the same thing. I was intending the first piece to produce the output of the second

a=Array.new(5,Array.new(3))
for i in (0...a[0].length)
  a[0][i]=2
end
p a

# this prints [[2, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2]]*

a=Array.new(5).map{|d|d=Array.new(3)}
for i in (0...a[0].length)
  a[0][i]=2
end
p a

# this prints [[2, 2, 2], [nil, nil, nil], [nil, nil, nil], [nil, nil, nil], [nil, nil, nil]]

  • 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-09T12:13:04+00:00Added an answer on June 9, 2026 at 12:13 pm

    This one

    a=Array.new(5,Array.new(3))
    

    Creates an array that contains the same array object within it five times. It’s kinda like doing this:

    a = []
    b = a
    a[0] = 123
    puts b[0] #=> 123
    

    Where this one:

    a=Array.new(5).map{ Array.new(3) }
    

    Creates a new 3 item array for each item in the parent array. So when you alter the first item it doesn’t touch the others.

    This is also why you shouldn’t really use the Array and Hash constructor default arguments, as they don’t always work they way you might expect.

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

Sidebar

Related Questions

I have two pieces of C++ code running on 2 different cores. Both of
Here are three pieces of similar code, the first two run fine, the last
I have a piece of code, which I am not sure how to refactor..
We have several projects in development sharing the same codebase. Certain pieces of code
In that piece of code, I have both NSLog that says dict has a
What is the difference between these two pieces of code? Both are working perfectly,
Typically I see these two pieces of code all around. Both works in my
I am working on some strange piece of code ,For me its not good
I have a small piece of code that produce PrimeFaces dropdown List p:selectOneMenu ,
Please help me in understanding this piece of code. var person = { 'first-name':

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.