Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to duplicate a object changing the class without attributesmemory copy

Reply
Thread Tools

How to duplicate a object changing the class without attributesmemory copy

 
 
Gennady Bystritsky
Guest
Posts: n/a
 
      04-15-2008
I=F1aki Baz Castillo wrote:
> 2008/4/15, Gennady Bystritsky <(E-Mail Removed)>:
>> Not true. To verify it, after you do "from.name =3D header.name", try
>> this:
>>
>> puts header.name.object_id
>> puts from.name.object_id
>>
>> You will see same object id, meaning that you have a reference, not
>> a new object.

>
> Ohh, that's annoying for me!
> You are right, both are the same objects **until** I modify
> one of them. I mean:
>
> from.name =3D header.name
> =3D> "AAAA"
> from.name. object_id =3D=3D header.name.object_id
> =3D> true
>
> So in this moment from.name. object_id =3D=3D header.name.object_id. But
> if I change the value:
>
> from.name =3D "BBBB"
> from.name. object_id =3D=3D header.name.object_id
> =3D> false
>
> Then from.name. object_id !=3D header.name.object_id, so then there are
> two values in memory at same time.
>
> Regards.


You do not change the value, you just assign a reference to a new string. T=
o change the content of a string object you need to use replace() on it. It=
looks like it omits you that "AAAA" and "BBBB" make string objects already=
and not just when you assign them to a variable.

Gennady.

 
Reply With Quote
 
 
 
 
Iñaki Baz Castillo
Guest
Posts: n/a
 
      04-15-2008
MjAwOC80LzE1LCBHZW5uYWR5IEJ5c3RyaXRza3kgPEdlbm5hZH kuQnlzdHJpdHNreUBxdWVzdC5j
b20+OgoKPiBZb3UgZG8gbm90IGNoYW5nZSB0aGUgdmFsdWUsIH lvdSBqdXN0IGFzc2lnbiBhIHJl
ZmVyZW5jZSB0byBhIG5ldyBzdHJpbmcuIFRvIGNoYW5nZSB0aG UgY29udGVudCBvZiBhIHN0cmlu
ZyBvYmplY3QgeW91IG5lZWQgdG8gdXNlIHJlcGxhY2UoKSBvbi BpdC4gSXQgbG9va3MgbGlrZSBp
dCBvbWl0cyB5b3UgdGhhdCAiQUFBQSIgYW5kICJCQkJCIiBtYW tlIHN0cmluZyBvYmplY3RzIGFs
cmVhZHkgYW5kIG5vdCBqdXN0IHdoZW4geW91IGFzc2lnbiB0aG VtIHRvIGEgdmFyaWFibGUuCgpU
aGFua3MgdG8gYm90aCwgSSd2ZSB1bmRlcnN0b29kIG5vdzoKCm E9IkFBQUEiCj0+ICJBQUFBIgoK
YS5vYmplY3RfaWQKPT4gLTYwNjA1NjEzOAoKYT0iQkJCQiIKPT 4gIkJCQkIiCgphLm9iamVjdF9p
ZAo9PiAtNjA2MDYzNDk4CgoKVGhhbmtzIDopCgoKLS0gCknDsW FraSBCYXogQ2FzdGlsbG8KPGli
Y0BhbGlheC5uZXQ+Cg==

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Object-arrays - duplicate object-references Are Nybakk Javascript 11 04-12-2008 06:05 PM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM
Changing a Character-object without creating a new object (?) Jesper Sahner Java 4 01-18-2005 02:48 PM



Advertisments