From: Mathieu Bouchard Date: 2001-08-12T14:55:30+09:00 Subject: [ruby-talk:19575] Re: Perl/Python/Ruby common backend (Parrot, can Ruby play too?) On Fri, 10 Aug 2001, Ned Konz wrote: > could load on demand. However, when I re-load an object that I had a proxy > to, I want all the other objects that pointed to the proxy to now point to > the real object. > Being able to swap their identities allows me to do this. > This is a trivial operation on the old Smalltalks that used double > indirection to get to objects. Patch below. ----------------8<----------------cut-here----------------8<---------------- diff -u -u -r1.51 object.c --- object.c 2001/05/30 09:12:34 1.51 +++ object.c 2001/08/12 05:51:57 @@ -33,6 +33,20 @@ static ID inspect; static ID clone; +VALUE rb_obj_become(VALUE self, VALUE other) { + long t[5]; + if (IMMEDIATE_P(self)) + rb_raise(rb_eTypeError, "%s is not boxed", + rb_class2name(CLASS_OF(self))); + if (IMMEDIATE_P(other)) + rb_raise(rb_eTypeError, "%s is not boxed", + rb_class2name(CLASS_OF(other))); + memcpy((long *)t ,(long *)self ,5*sizeof(long)); + memcpy((long *)self ,(long *)other,5*sizeof(long)); + memcpy((long *)other,(long *)t ,5*sizeof(long)); + return self; +} + VALUE rb_equal(obj1, obj2) VALUE obj1, obj2; @@ -1296,4 +1310,6 @@ eql = rb_intern("eql?"); inspect = rb_intern("inspect"); clone = rb_intern("clone"); + + rb_define_method(rb_mKernel, "become", rb_obj_become, 1); } ----------------8<--------------d�coupez-ici--------------8<---------------- a = [6,6,6] b = {42,13} p a #=> [6, 6, 6] p b #=> {42=>13} a.become b p a #=> {42=>13} p b #=> [6, 6, 6] a.become b p a #=> [6, 6, 6] p b #=> {42=>13} ----------------8<--------------d�coupez-ici--------------8<---------------- matju