Case-insensitive hash?

So I understand that a hash can use keys of any type, not just string
(or
symbol), but there are occasions when I have a hash that has been
populated
with string keys whose values I would like to access case-insensitively.
Is
there a setting or something? Do I have to write a wrapper to deal with
it?

–Greg

On 06/01/06, Gregory S. [email protected] wrote:

So I understand that a hash can use keys of any type, not just string (or
symbol), but there are occasions when I have a hash that has been populated
with string keys whose values I would like to access case-insensitively. Is
there a setting or something? Do I have to write a wrapper to deal with it?

You will need to write a wrapper. Look at HashWithIndifferentAccess in
Rails for an example of how to do this.

-austin

On 2006.01.07 05:05, Gregory S. wrote:

So I understand that a hash can use keys of any type, not just string (or
symbol), but there are occasions when I have a hash that has been populated
with string keys whose values I would like to access case-insensitively. Is
there a setting or something? Do I have to write a wrapper to deal with it?

If you always access that particular Hash case-insensitively, then
the simplest solution would be to filter input to it:

@hash[key.downcase] = value

If, on the other hand, you alter between sensitive and insensitive,
you would filter the output instead

(Naively)

nil unless @hash.keys.find {|k| key.downcase == k.downcase}
@hash[key]

If you need to, you could encapsulate this behaviour in a subclass.

–Greg

E