Search code examples
arraysruby-on-railsrubyhashruby-hash

Reflecting value of hash in Array Rails


I have one Array.

arr = []

I have one Hash

hash = {a: 1, b: 2, c: 3}

Addind hash in Array

arr << hash

value of arr is:

[{:a=>1, :b=>2, :c=>3}] 

Now Adding value in Hash

hash[:d] = 4

Now See Value of Array is:

[{:a=>1, :b=>2, :c=>3, :d=>4}]

Can anyone please explain to me about this. as this is a little confusing. Thank in Advance.


Solution

  • Because hash inside the array also has the same object_id, let's try it with an example

    2.6.3 :008 > arr = []
     #=> [] 
    2.6.3 :009 > hash = {a: 1, b: 2, c: 3}
     #=> {:a=>1, :b=>2, :c=>3} 
    2.6.3 :010 > arr << hash
     #=> [{:a=>1, :b=>2, :c=>3}] 
    2.6.3 :011 > arr.first.object_id
     #=> 70240020878720 
    2.6.3 :012 > hash.object_id
     #=> 70240020878720
    

    As you can see, arr.first.object_id and hash.object_id both have same object_id therefore any changes you make in hash will also be reflected in hash inside arr because it's the same object.

    Now if you don't want to see that behavior then create a new object instead, use dup, try this

    2.6.3 :001 > arr = []
     #=> [] 
    2.6.3 :002 > hash = {a: 1, b: 2, c: 3}
     #=> {:a=>1, :b=>2, :c=>3} 
    2.6.3 :003 > arr << hash.dup
     #=> [{:a=>1, :b=>2, :c=>3}] 
    2.6.3 :004 > arr.first.object_id
     #=> 70094898530860 
    2.6.3 :005 > hash.object_id
     #=> 70094898418620 
    2.6.3 :006 > hash[:d] = 4
     #=> 4 
    2.6.3 :007 > arr
     #=> [{:a=>1, :b=>2, :c=>3}] 
    2.6.3 :008 > hash
     #=> {:a=>1, :b=>2, :c=>3, :d=>4} 
    

    dup creates a new object and hence you won't see the changes made in both the places because both are different object with different object_id

    Hope that helps!