From: "Berger, Daniel" Date: 2006-07-25T23:13:25+09:00 Subject: Re: Hash order bug? > -----Original Message----- > From: Javier Valencia [mailto:jvalencia@log01.org] > Sent: Tuesday, July 25, 2006 7:48 AM > To: ruby-talk ML > Subject: Re: Hash order bug? > > > 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? No, it's just the way hashes behave. Note that there are ordered hash implementations on the RAA: http://raa.ruby-lang.org/project/orderedhash/ You can also use a Struct to get roughly the same effect. Regards, Dan This communication is the property of Qwest and may contain confidential or privileged information. Unauthorized use of this communication is strictly prohibited and may be unlawful. If you have received this communication in error, please immediately notify the sender by reply e-mail and destroy all copies of the communication and any attachments.