From: ruby-talk.closure@... Date: 2006-07-26T05:07:14+09:00 Subject: Re: Hash order bug? ------=_Part_208754_4764418.1153857427286 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Javier, I don't believe it's a missing feature. In every language I've used, Hashes are not ordered. It has to do with how they are implemented under the hood. If you really need an ordered Hash, you could create a class that uses an Array to maintain the order of the keys, and store the values in a Hash. /Doug On 7/25/06, Javier Valencia - jvalencia@log01.org <+ruby- talk+closure+a5facb16dd.jvalencia#log01.org@spamgourmet.com> wrote: > > dblack@wobblini.net wrote: > > > Hi -- > > > > On Tue, 25 Jul 2006, Javier Valencia wrote: > > > >> I have this piece of simple code: > >> > >> ---------------------------------------------- > >> def foo > >> return 5 > >> end > >> > >> a = {:gr => false, :und => false, :det => true, :sta => false, :inv > >> => false} > >> puts a.inspect > >> ---------------------------------------------- > >> > >> when I execute it i get a totally unordered hash: > >> > >> ---------------------------------------------- > >> ruby pro.rb > >> {:inv=>false, :gr=>false, :und=>false, :det=>true, :sta=>false} > >> ---------------------------------------------- > >> > >> Now, i delete the foo function from the code (the foo function don't > >> do nothing at all), and i get a well ordered hash: > >> > >> ---------------------------------------------- > >> ruby pro.rb > >> {:gr=>false, :und=>false, :det=>true, :sta=>false, :inv=>false} > >> ---------------------------------------------- > >> > >> > >> What's happening? all my code is behaving wrong because of that. > > > > > > Hashes are unordered. If you need an ordered collection, you'll need > > to use an array. > > > > > > David > > > > Oh my god, i didn't know it, sorry. Is that a missing feature? > > ------=_Part_208754_4764418.1153857427286--