From: Mauricio Fernandez Date: 2006-08-14T01:16:14+09:00 Subject: Re: A use case for an ordered hash On Mon, Aug 14, 2006 at 12:27:07AM +0900, James Edward Gray II wrote: > On Aug 13, 2006, at 3:25 AM, Robert Klemme wrote: > >Hal Fulton wrote: > >>Once I wanted a "dynamic case statement" of sorts. > >>I wanted to use procs as values in a hash. Something > >>like: > >> actions = { /abcd/ => lambda { do_this }, > >> /xyz/ => lambda { do_that }, > >> /abc/ => lambda { other }} [...] > >To me a Hash feels wrong. Why? Because you don't make any use of > >hash properties for fast lookup. You just iterate in plain order. > > He *is* making use of a Hash property. He wants the keys to be > unique. None of the other solutions shown in this thread have > addressed that. Hal is making the case for *literal* "ordered hashes". The keys will be unique because he's writing them himself manually. I don't think he wants to make use of this: actions = { /a/ => 1, # <-- /b/ => 2, # \ /a/ => 4 # <--+-- why do this in the first place. } actions[/a/] # => 4 The way he uses the structure is reminiscent of Array#assoc. The only thing a Hash has going for it in this case is the cleaner notation and the arrow. But actions = ASSOC [/abcd/, lambda{ ... }, /abc/, lambda{ ... } ] (using parentheses if you want) looks good enough for me, and something like the Array#cassoc I showed before would do fine in this case. -- Mauricio Fernandez - http://eigenclass.org - singular Ruby