From: David Alan Black Date: 2001-12-12T00:21:38+09:00 Subject: [ruby-talk:28228] Re: Hash.keys Q Hello -- On Tue, 11 Dec 2001, Leo Laursen wrote: > Hi all > > I have written my first ruby program, and it was fun, but there is one > litle detail i'm wondering about. Welcome to Ruby! > The program logs on/off to my isp via a slightly modified telnet > function. The options for "telnet" is a hash read from a file. > > File.foreach(conffile){ |l| options[$1]= $2 if l ~= /(.*):\s+(.*)/ } > the conffile looks like this: > > Host: IP-addr. > Port: portnr. > Name: username > Password: password > > Now options.keys returns ["Password", "Host", "Port", "Name"] > > I would expect ["Host", "Port", "Name", "Password"] > > is there an explanation for this ? Yes: the order of hash keys is not guaranteed. I *think* #keys returns the keys in the same order when called multiple times, but I'm not 100% sure. In any case, that order itself cannot be predicted. I believe various people have written various OrderedHash extensions... (very helpful advice, I know :-) You could search on to see if there's been discussion here. David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav