From: Jeremy Henty Date: 2007-02-20T18:30:12+09:00 Subject: Re: Why should initialize_copy verify the class of the original? On 2007-02-19, David Vallner wrote: > On Sun, 18 Feb 2007 17:10:06 +0100, Jeremy Henty > wrote: > >> [...] If #initialize_copy is only called from inside #dup or #clone >> then surely the receiver has to be of the same class as the >> original! What am I missing? > > CDPlayer.new.instance_eval {initialize_copy(TeddyBear.new)} That's the conclusion we came to in the other thread (though it wasn't written down explicitly as you did here). > Which would probably cause the C implementation to do Bad Things > without said check. Yes, if the instances of the CDPlayer and TeddyBear classes are T_DATA objects that wrap (CDPlayer *) and (TeddyBear *) pointers, then this code will coerce a (TeddyBear *) to a (CDPlayer *). Hilarity may well ensue. Incidentally the Pickaxe is (IMHO) a little misleading on this point, it describes Data_Get_Struct as "a type-safe wrapper around the macro DATA_PTR(obj)". In fact it's only type-safe with respect to the TYPE of obj , it's not type-safe with respect to the type of the C pointer that obj wraps. Regards, Jeremy Henty