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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:39:13+00:00 2026-06-14T14:39:13+00:00

dup is shallow copy, so when doing this: h = {one: {a:’a’, b: ‘b’}}

  • 0

dup is shallow copy, so when doing this:

h = {one: {a:'a', b: 'b'}}
h_copy = h.dup
h_copy[:one][:b] = 'new b'

now h and h_copy is same: {:one=>{:a=>"a", :b=>"new b"}}
yes, that right.

But when h is a one dimension hash:

h = {a:'a', b: 'b'}
h_copy = h.dup
h_copy[:b] = 'new b'
h still is: {a:'a', b: 'b'}
h_copy is {a:'a', b: 'new b'}

Why?

  • 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-14T14:39:14+00:00Added an answer on June 14, 2026 at 2:39 pm

    So after 1 hour of brainstorming..I have come to the conclusion that in the multi dimensional hashes, the dup generates the same object_id for each key which is in turn referring to the hash whereas in the single dimensional hash, the object_ids are similar initially but when we make any changes to the object the Ruby would assign new object_id to the hash keys..

    Look at the following code

    h = { :a => "a", :b => "b" } # => {:a=>"a", :b=>"b"} 
    h_clone = h.dup #=> {:a=>"a", :b=>"b"} 
    h.object_id #=> 73436330 
    h_clone.object_id #=> 73295920 
    h[:a].object_id #=> 73436400 
    h_clone[:a].object_id #=> 73436400 
    h[:b].object_id #=> 73436380 
    h_clone[:b].object_id #=> 73436380 
    h_clone[:b] = "New B" #=> "New B" 
    h_clone[:b].object_id #=> 74385280 
    h.object_id #=> 73436330 
    h_clone.object_id #=> 73295920 
    h[:a].object_id #=> 73436400 
    h_clone[:a].object_id #=> 73436400
    

    Look the following code for the multidimensional array

    h = { :one => { :a => "a", :b => "b" } } #=> {:one=>{:a=>"a", :b=>"b"}} 
    h_copy = h.dup #=> {:one=>{:a=>"a", :b=>"b"}} 
    h_copy.object_id #=> 80410620 
    h.object_id #=> 80552610 
    h[:one].object_id #=> 80552620 
    h_copy[:one].object_id #=> 80552620 
    h[:one][:a].object_id #=> 80552740 
    h_copy[:one][:a].object_id #=> 80552740 
    h[:one][:b].object_id #=> 80552700 
    h_copy[:one][:b].object_id #=> 80552700 
    h_copy[:one][:b] = "New B" #=> "New B" 
    h_copy #=> {:one=>{:a=>"a", :b=>"New B"}} 
    h #=> {:one=>{:a=>"a", :b=>"New B"}} 
    h.object_id #=> 80552610 
    h_copy.object_id #=> 80410620 
    h[:one].object_id #=> 80552620 
    h_copy[:one].object_id #=> 80552620 
    h[:one][:b].object_id #=> 81558770 
    h_copy[:one][:b].object_id #=> 81558770
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if this is a dup; I haven't found any questions that pose quite
I have this code. byte dup = 0; Encoding.ASCII.GetString(new byte[] { (0x80 | dup)
To begin with, this question is not a dup of this one , but
Now before people start marking this a dup, I've read all the following, none
I have this byte code: new java.lang.Object // stack is [newObjectRef] dup // Stack
I know that dup, dup2, dup3 create a copy of the file descriptor oldfd
I'm trying to instantiate a class in Jasmin like this: new Ljava/lang/Object; dup invokespecial
This is probably not a dup; I have read through many similar problems on
This is probably a dup (but did not find it..) When you type ls
Basically a dup of this question using php , but I need it for

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.