From: Gavin Sinclair Date: 2002-09-24T15:43:41+09:00 Subject: Re: Checking hash key's and values, with case insensitivity ----- Original Message ----- From: > Hi -- > > On Tue, 24 Sep 2002, Gavin Sinclair wrote: > > > ----- Original Message ----- > > From: "Yukihiro Matsumoto" > > > > > > > > |How can I check hash keys and values without case sensitivity? > > > |Specifically I'm referring to the hash ".has_key?" and ".has_value?" > > > |methods. I believe these are case sensitive by default. > > > > > > Why not simply > > > > > > h.key?(k.upcase) > > > h.value?(v.upcase) > > > > > > matz. > > > > This has got to be the best solution, as it takes account of the important fact > > that noone else has yet mentioned: Hash keys are not necessarily Strings. > > What if the key you used is lowercase? like this: > > irb(main):002:0> h = {"a" => 1} > {"a"=>1} > irb(main):003:0> k = "a" > "a" > irb(main):004:0> h.key?(k) > true > irb(main):005:0> h.key?(k.upcase) > false > I think Matz intended that the keys get case-neutralised upon entry into the hash. Naturally, that stuffs you if you input both "Key" and "kEy" into the hash, but as someone pointed out, if genuine case-insensitive retrieval is what you want, then hash is not what you need. May I suggest a binary tree or similar structure, in case the original poster is still interested? Gavin