# How to do explicit copy

**URL:** <https://www.ruby-forum.com/t/how-to-do-explicit-copy/176819>\
**Category:** Ruby\
**Created:** [October 6, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/how-to-do-explicit-copy/176819 "2009-10-06T17:45:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rickyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ed655f/32.png) [@rickyr](https://www.ruby-forum.com/u/rickyr)\
**Post date:** [October 6, 2009, 5:45pm UTC](https://www.ruby-forum.com/t/how-to-do-explicit-copy/176819/1 "2009-10-06T17:45:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rickyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ed655f/32.png) [@rickyr](https://www.ruby-forum.com/u/rickyr)\
**Post date:** [October 6, 2009, 5:51pm UTC](https://www.ruby-forum.com/t/how-to-do-explicit-copy/176819/2 "2009-10-06T17:51:40Z")

</div>

On Oct 6, 2009, at 6:45 PM, rickyr wrote:

> y = x

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

---

<div class="post-metadata">

**Author:** ![rickyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ed655f/32.png) [@rickyr](https://www.ruby-forum.com/u/rickyr)\
**Post date:** [October 8, 2009, 5:56am UTC](https://www.ruby-forum.com/t/how-to-do-explicit-copy/176819/3 "2009-10-08T05:56:03Z")

</div>

On Oct 6, 10:50 pm, Patrick O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Oct 6, 2009, at 6:45 PM, rickyr wrote:
> 
> > y = x
> 
> y = x.dup may be sufficient for you (it does a shallowcopy).
> 
> –  
> patrick

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

---

<div class="post-metadata">

**Author:** ![rickyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ed655f/32.png) [@rickyr](https://www.ruby-forum.com/u/rickyr)\
**Post date:** [October 8, 2009, 6:40am UTC](https://www.ruby-forum.com/t/how-to-do-explicit-copy/176819/4 "2009-10-08T06:40:32Z")

</div>

rickyr wrote:

> and object#clone. Both doing shallow 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?©  
=\> true  
irb(main):006:0\> a.equal?(b)  
=\> false  
irb(main):007:0\> a.equal?©  
=\> true  
irb(main):008:0\>

---

<div class="post-metadata">

**Author:** ![rickyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ed655f/32.png) [@rickyr](https://www.ruby-forum.com/u/rickyr)\
**Post date:** [October 8, 2009, 9:57am UTC](https://www.ruby-forum.com/t/how-to-do-explicit-copy/176819/5 "2009-10-08T09:57:10Z")

</div>

2009/10/8 rickyr [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> On Oct 6, 10:50 pm, Patrick O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > On Oct 6, 2009, at 6:45 PM, rickyr wrote:
> > 
> > > y = x
> > 
> > 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.

It’s in the docs:

ri Object#clone Object#dup

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

Cheers

robert

---

<div class="post-metadata">

**Author:** ![rickyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ed655f/32.png) [@rickyr](https://www.ruby-forum.com/u/rickyr)\
**Post date:** [October 8, 2009, 12:55pm UTC](https://www.ruby-forum.com/t/how-to-do-explicit-copy/176819/6 "2009-10-08T12:55:41Z")

</div>

rickyr wrote:

> 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
