From: Charles Mills Date: 2004-07-19T00:18:16+09:00 Subject: Re: DUPSETUP On Jul 18, 2004, at 12:02 AM, daz wrote: > > Charles Mills wrote: >> >> I was wondering what the purpose of the DUPSETUP() macro is in ruby.h. >> I didn't find anything about it using google. >> What is it intended to be used for? >> -Charlie >> > > I guess it may, eventually, be replaced by rb_obj_init_copy > (Object#initialize_copy) referred to in the #dup ri entry [1], > but I'll have a shot at describing what it _was_ for - using > the last remaining example from the source distribution in > array.c: > > VALUE > rb_ary_dup(ary) > VALUE ary; > { > VALUE dup = rb_ary_new2(RARRAY(ary)->len); > > DUPSETUP(dup, ary); > MEMCPY(RARRAY(dup)->ptr, RARRAY(ary)->ptr, VALUE, > RARRAY(ary)->len); > RARRAY(dup)->len = RARRAY(ary)->len; > return dup; > } > > "dup" (an empty array with same length) is created (with default state > for a new array and no associated instance variables). > > DUPSETUP copies some state flags and any associated instance variables. > Thanks for the response. Can DUPSETUP() be used for objects with type T_DATA? -Charlie > MEMCPY copies the object pointers (contents), then the length update > seems to be redundant ( unchanged since rb_ary_new2() ??). > > > :daz > > > [1] > ------------------------------------------------------------- > Object#dup > obj.dup -> an_object > ----------------------------------------------------------------------- > - > Produces a shallow copy of _obj_---the instance variables of _obj_ > are copied, but not the objects they reference. +dup+ copies the > tainted state of _obj_. See also the discussion under > +Object#clone+. In general, +clone+ and +dup+ may have different > semantics in descendent classes. While +clone+ is used to > duplicate > an object, including its internal state, +dup+ typically uses the > class of the descendent object to create the new instance. > > This method may have class-specific behavior. If so, that behavior > will be documented under the #+initialize_copy+ method of the > class. > > ----------------------------------------------------------------------- > - > > >