From: Jacob Fugal Date: 2006-05-06T01:33:38+09:00 Subject: Re: Constant in Ruby. On 5/5/06, Charlie wrote: > Thank you everyone for your input. I got this simple example to share: > > A = 10 => 10 > X = A.freeze => 10 > A = 20 => 20 > X => 10 Check the following irb session to see what's happening: >> A = 10 => 10 >> A.object_id => 21 >> X = A.freeze => 10 >> A.object_id => 21 >> X.object_id => 21 >> A = 20 warning: already initialized constant A => 20 >> A.object_id => 41 >> X.object_id => 21 >> X => 10 A and X are just references to objects. Setting X equal to A makes X reference the same object as A referenced at the time, but X does not reference A directly. So changing what A references (assignment) has no impact on X -- X just keeps on referencing what it was told to reference. However, if A and X continue to reference the same object and you perform an in place modification on either, both will see the change since they still reference that modified object: >> A = "foo" => "foo" >> A.object_id => 1661894 >> X = A => "foo" >> A.object_id => 1661894 >> X.object_id => 1661894 >> A << "bar" => "foobar" >> A.object_id => 1661894 >> X.object_id => 1661894 >> X => "foobar" Jacob Fugal