[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Case-insensitive hash?

Gregory Seidman

1/6/2006 8:05:00 PM

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



2 Answers

Austin Ziegler

1/6/2006 8:29:00 PM

0

On 06/01/06, Gregory Seidman <gsslist+ruby@anthropohedron.net> 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
--
Austin Ziegler * halostatue@gmail.com
* Alternate: austin@halostatue.ca


ES

1/6/2006 10:00:00 PM

0

On 2006.01.07 05:05, Gregory Seidman 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