Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!talisker.lacave.net!lacave.net!not-for-mail From: Kevin Mahler Newsgroups: comp.lang.ruby Subject: Re: Get the real object in a Hash key Date: Fri, 15 Apr 2011 09:47:22 -0500 Organization: Service de news de lacave.net Lines: 29 Message-ID: References: NNTP-Posting-Host: bristol.highgroove.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: talisker.lacave.net 1302879409 7681 65.111.164.187 (15 Apr 2011 14:56:49 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Fri, 15 Apr 2011 14:56:49 +0000 (UTC) In-Reply-To: X-Received-From: This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway X-Mail-Count: 381637 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:2948 Robert K. wrote in post #993000: > On Fri, Apr 15, 2011 at 3:14 PM, Iaki Baz Castillo >> Given a string "aaa", how can I get the object k1 from the hash? (I >> mean without comparing String#eql? each key with the string "aaa") >> Unfortunatelly I think Hash class does not provide a method for it. > > Exactly. And you don't want to do it. A Hash is an associative > storage which associates the value with your key. If you need to > stuff in more information - you need to add it to the value and not > the key.... Well you may want to do it -- that's why Hash#assoc exists. Hash keys can be objects of any sort, and there are use cases for storing nonsimple keys. The reason there's no constant-time equivalent of Hash#assoc is because hashing, by its very nature, cannot be reversed. There's no method for it because one cannot possibly exist. It's not because one should never be interested in the key object. Hash#assoc is there for a reason. Lispers will recognize assoc as relating to the Lisp function of the same name which has exactly that use case: key/value pairs where the key and the value matter as objects in their own right, apart from the the hashing function result. -- Posted via http://www.ruby-forum.com/.