Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #2948

Re: Get the real object in a Hash key

From Kevin Mahler <kevin.mahler@yahoo.com>
Newsgroups comp.lang.ruby
Subject Re: Get the real object in a Hash key
Date 2011-04-15 09:47 -0500
Organization Service de news de lacave.net
Message-ID <de78d150786e2caeed0186e184fd2ae9@ruby-forum.com> (permalink)
References <BANLkTin0FvFkx+kfvXuMxp3_7XkhqkiA_w@mail.gmail.com> <BANLkTi=dvQxGfycrsoyWUKchPQBMcybS0w@mail.gmail.com> <BANLkTikWQQfzFCjYx8SXtX2WREZ70AAodg@mail.gmail.com> <BANLkTi=0Xt-DpOzu9pK_3Ac4JoGD3cmNtA@mail.gmail.com>

Show all headers | View raw


Robert K. wrote in post #993000:
> On Fri, Apr 15, 2011 at 3:14 PM, Iaki Baz Castillo <ibc@aliax.net>
>> 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/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Get the real object in a Hash key Iñaki Baz Castillo <ibc@aliax.net> - 2011-04-15 07:50 -0500
  Re: Get the real object in a Hash key Robert Klemme <shortcutter@googlemail.com> - 2011-04-15 08:00 -0500
    Re: Get the real object in a Hash key Iñaki Baz Castillo <ibc@aliax.net> - 2011-04-15 08:14 -0500
      Re: Get the real object in a Hash key Robert Klemme <shortcutter@googlemail.com> - 2011-04-15 08:48 -0500
        Re: Get the real object in a Hash key Iñaki Baz Castillo <ibc@aliax.net> - 2011-04-15 09:46 -0500
        Re: Get the real object in a Hash key Kevin Mahler <kevin.mahler@yahoo.com> - 2011-04-15 09:47 -0500
          Re: Get the real object in a Hash key Robert Klemme <shortcutter@googlemail.com> - 2011-04-15 11:08 -0500
            Re: Get the real object in a Hash key Kevin Mahler <kevin.mahler@yahoo.com> - 2011-04-15 12:39 -0500
              Re: Get the real object in a Hash key Robert Klemme <shortcutter@googlemail.com> - 2011-04-16 15:20 +0200
              Re: Get the real object in a Hash key Iñaki Baz Castillo <ibc@aliax.net> - 2011-04-16 09:51 -0500
                Re: Get the real object in a Hash key Robert Klemme <shortcutter@googlemail.com> - 2011-04-16 18:15 +0200
                Re: Get the real object in a Hash key Iñaki Baz Castillo <ibc@aliax.net> - 2011-04-16 11:55 -0500
                Re: Get the real object in a Hash key Josh Cheek <josh.cheek@gmail.com> - 2011-04-16 11:17 -0500
  Re: Get the real object in a Hash key jake kaiden <jakekaiden@yahoo.com> - 2011-04-16 10:28 -0500

csiph-web