Re: str1 = str2 is not a copy?!?

From: Gregory B. [mailto:[email protected]]

p array # array has been changed

I get the point of this example, but it’s worth noting that you can
change the array via b if you just use String#replace

Wording like that promotes dangerous mental models, IMO.

NOTHING about the array itself changes. It still refers to exactly the
same objects as it did before. It’s just that you happened to have one
of the objects that it refers to change.

Consider the example:

Person = Struct.new( :name, :age )
gavin = Person.new( ‘Gavin’, 33 )
lisa = Person.new( ‘Lisa’, 32 )

people = [ gavin, lisa ]

someone = people[ 0 ]
someone.age = 600

In the above, I am changing the ‘Gavin’ person to have a new age.
Because the first element in the ‘people’ array is that same object, you
might say that the array ‘changed’. It would be better not to think of
it this way, however. The array STILL refers to Gavin and Lisa.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs