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>