How to do explicit copy

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

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

···

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

y = x

--
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

···

On Oct 6, 10:50 pm, Patrick Okui <po...@psg.com> 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

rickyr wrote:

···

On Oct 6, 10:50 pm, Patrick Okui <po...@psg.com> 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

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>

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 `<main>'
irb(main):004:0> t = s.dup
=> "foo"
irb(main):005:0> t << "ho!"
=> "fooho!"
irb(main):006:0> t
=> "fooho!"

Cheers

robert

···

2009/10/8 rickyr <rickyrm978@gmail.com>:

On Oct 6, 10:50 pm, Patrick Okui <po...@psg.com> 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.

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

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

···

--
Posted via http://www.ruby-forum.com/\.