From: Brian Adkins Date: 2008-12-18T05:07:16+09:00 Subject: Re: why Hash corrupts 'key' object ? Mike Gold writes: > Brian Adkins wrote: >> >> What the above demonstrates is that dup'ing and freezing a TestStr >> does not nilify TestStr#dupstr, so the C code for Hash seems to be >> doing more than just dup'ing and freezing. Does that make sense? >> > > Yes, sorry, I misunderstood. I did re-read your post several times but > was still a little puzzled. > > You are right that something appears to be amiss. rb_str_new4() > duplicates the String portion of the TestStr object, but does not copy > over the TestStr-specific data. A subclassed String is not duplicated; > the documentation is wrong. > > I see the reason for it: the raw string data is being shared between the > object and its "dup". I guess sharing should only be done if it's a > real String and not a subclass thereof. This fixes the problem reported > in this thread. > > --- a/ruby-1.8.7-p72/hash.c 2008-06-08 14:25:01.000000000 -0400 > +++ b/ruby-1.8.7-p72/hash.c 2008-12-13 13:47:33.000000000 -0500 > @@ -986,7 +986,8 @@ > VALUE hash, key, val; > { > rb_hash_modify(hash); > - if (TYPE(key) != T_STRING || st_lookup(RHASH(hash)->tbl, key, 0)) { > + if (rb_obj_class(key) != rb_cString || > + st_lookup(RHASH(hash)->tbl, key, 0)) { > st_insert(RHASH(hash)->tbl, key, val); > } > else { Wow, complete with a patch! Have you passed this on to the appropriate channel for possible inclusion ? > -- > Posted via http://www.ruby-forum.com/. > -- Brian Adkins http://www.lojic.com/ http://lojic.com/blog/