From: "Iñaki Baz Castillo" Date: 2011-04-16T23:51:46+09:00 Subject: Re: Get the real object in a Hash key 2011/4/15 Kevin Mahler : > He has a key. It contains some > data. It's not necessarily true that he should duplicate that data in > the mapped-to values. To clarify, my exact case is the following: I've coded a parser for SIP (similar to HTTP). The parser generates a Request object which inherits from Hash, and each SIP request header (i.e. "From: sip:alice@example.org") becomes an entry of the hash (Request object) as follows: - The key is "FROM" (capitalized). - The value is an Array of strings (a s header can have multiple values). I need to store the key capitalized for fastest lookup, but I also want to store the original header name (which could be "from", "From", "frOM" and so). So my parser adds an instance variable @real_name within the header name string ("FROM"). When I do the lookup of a header in the Request object, I would like also to retrieve the key's @real_name, but I've already understood that this is only possible if taint the key string before inserting it in the hash and use Hash#assoc. This solution is not good for performance. The solution suggested by Robert is adding such information (the header original name) as a field in the hash entry value, so instead of having: request["FROM"] => [ "sip:alice@xample.org ] I would end with something like: request["FROM"] => Struct ( "From", [ "sip:alice@xample.org ] ) The problem this last suggestion introduces is that it breaks the existing API and makes more complext for a developer to handle the Request class (which should be as easy as handling a Hash). Thanks to both for your comments. -- Iñaki Baz Castillo