How to do explicit copy

R

rickyr

Hi,

I notice Ruby uses reference copy by default. The problem is I need to
keep two variables keep separate copies of the same data.

If I do this:

x = Hash.new
# fill a with data

y = x
# delete some keys from x

I got those same keys deleted from y ('cos x & y reference the same
data, I believe). How to tell ruby that I want y keep a separate copy
of x's data, so that if I do anything to x, y is not effected.

TIA
 
R

rickyr

y = x.dup may be sufficient for you (it does a shallowcopy).

Thanks, it worked. Now, what are the differences between object#dup
and object#clone. Both doing shallow copy.

Also, when I do this

a = Hash.new
puts(a.id)

Ruby whines no such method existed. The docs mentioned object#id, and
all are descendant of object class. What's wrong? How can I verify
(other than a handful of puts) if indeed two objects do not share the
same data, but each has own copy?

TIA
 
S

steve

rickyr said:
Thanks, it worked. Now, what are the differences between object#dup
and object#clone. Both doing shallow copy.

Also, when I do this

a = Hash.new
puts(a.id)

Ruby whines no such method existed. The docs mentioned object#id, and
all are descendant of object class. What's wrong? How can I verify
(other than a handful of puts) if indeed two objects do not share the
same data, but each has own copy?

TIA

Ricky, there is equal? method for just this purpose

C:\Documents and Settings\Administrator>ri Object#eql
------------------------------------------------------------ Object#eql?
obj == other => true or false
obj.equal?(other) => true or false
obj.eql?(other) => true or false
------------------------------------------------------------------------
Equality---At the +Object+ level, +==+ returns +true+ only if _obj_
and _other_ are the same object. Typically, this method is
overridden in descendent classes to provide class-specific meaning.

Unlike +==+, the +equal?+ method should never be overridden by
subclasses: it is used to determine object identity (that is,
+a.equal?(b)+ iff +a+ is the same object as +b+).

The +eql?+ method returns +true+ if _obj_ and _anObject_ have the
same value. Used by +Hash+ to test members for equality. For
objects of class +Object+, +eql?+ is synonymous with +==+.
Subclasses normally continue this tradition, but there are
exceptions. +Numeric+ types, for example, perform type conversion
across +==+, but not across +eql?+, so:

1 == 1.0 #=> true
1.eql? 1.0 #=> false


in ruby 1.8.6 under windows ...

irb(main):001:0> a="this is a string"
=> "this is a string"
irb(main):002:0> b="this is a string"
=> "this is a string"
irb(main):003:0> c=a
=> "this is a string"
irb(main):004:0> a.eql?(b)
=> true
irb(main):005:0> a.eql?(c)
=> true
irb(main):006:0> a.equal?(b)
=> false
irb(main):007:0> a.equal?(c)
=> true
irb(main):008:0>
 
R

Robert Klemme

2009/10/8 rickyr said:
Thanks, it worked. Now, what are the differences between object#dup
and object#clone. Both doing shallow copy.

It's in the docs:

ri Object#clone Object#dup

irb(main):001:0> s =3D "foo".freeze
=3D> "foo"
irb(main):002:0> t =3D s.clone
=3D> "foo"
irb(main):003:0> t << "ha!"
RuntimeError: can't modify frozen string
from (irb):3
from /opt/bin/irb19:12:in `<main>'
irb(main):004:0> t =3D s.dup
=3D> "foo"
irb(main):005:0> t << "ho!"
=3D> "fooho!"
irb(main):006:0> t
=3D> "fooho!"

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
S

Siep Korteling

rickyr said:
Also, when I do this

a = Hash.new
puts(a.id)

Ruby whines no such method existed. The docs mentioned object#id, and
all are descendant of object class.

irb(main):004:0> RUBY_VERSION
=> "1.8.6"
irb(main):005:0> a={}
=> {}
irb(main):006:0> a.id
(irb):6: warning: Object#id will be deprecated; use Object#object_id
=> 22862560
irb(main):007:0> a.object_id
=> 22862560

hth,

Siep
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,167
Messages
2,570,910
Members
47,453
Latest member
MadelinePh

Latest Threads

Top