From: Rick DeNatale Date: 2009-02-20T06:22:55+09:00 Subject: Re: Creating InsensitiveHash in C --0016e645aaccc38ed004634c24a8 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Thu, Feb 19, 2009 at 3:57 PM, I=F1aki Baz Castillo wrote= : > Hi, I'd like a faster Ruby class same as Hash but in which key and value > are > insensitive, so: > > ---------------------------------- > params =3D InsensitiveHash.new > params["q"] =3D 0.5 > params["transport"] =3D "TCP" > > params["Q"] > =3D> 0.5 > > params["Transport"] =3D=3D "tcp" > =3D> true > ---------------------------------- > > It will be used extensively so I need it being very fast. I'm thinking in > implementing it as a new Ruby class in C, modifying Hash class. > > Do you think it's feasible? Any suggestion? > Thanks a lot. > > PD: Where is the C file defining Hash class? I read "hash.c" in Hash > documentation: > http://www.ruby-doc.org/core/classes/Hash.html > but I can't find it in Ruby libs directory. > You won't find it in a runtime installation of Ruby. The file hash.h is in the top level directory of the Ruby source code. That said, If all you want to do is to make the keys case insensitive, a naive Ruby implementation is likely to be almost as fast as anything you could do in C, since the time intensitve implementation parts of Hash are already in C. So something like class InsensitiveHash def initialize @hash =3D {} end def [](key} @hash[key.downcase] end def []=3D(key, value) @hash[key.downcase] =3D value end end And yes, I'd recommend having the InsensitiveHash have an instance of Hash rather than being a subclass of Hash. --=20 Rick DeNatale Blog: http://talklikeaduck.denhaven2.com/ Twitter: http://twitter.com/RickDeNatale WWR: http://www.workingwithrails.com/person/9021-rick-denatale LinkedIn: http://www.linkedin.com/in/rickdenatale --0016e645aaccc38ed004634c24a8--