On Hashes - How the hashes printing works?

  • Thread starter Neela megha shyam Chivukula
  • Start date
N

Neela megha shyam Chivukula

Hi All,

I have my code as follows:-

people = {
"torvalds"=>{"lname"=>"Torvalds", "fname"=>"Linus",
"job"=>"maintainer"}, "matsumoto"=>{"lname"=>"Matsumoto",
"fname"=>"Yukihiro", "job"=>"Ruby originator"},
"litt"=>{"lname"=>"Litt", "fname"=>"Steve", "job"=>"troubleshooter"}
}
keys = people.keys
for key in 0...keys.length
print "key : ", keys[key], "\n"
print "lname: ", people[keys[key]]["lname"], "\n"
print "fname: ", people[keys[key]]["fname"], "\n"
print "job : ", people[keys[key]]["job"], "\n"
print "\n\n"
end

O/p:-
key : litt
lname: Litt
fname: Steve
job : troubleshooter
key : matsumoto
lname: Matsumoto
fname: Yukihiro
job : Ruby originator
key : torvalds
lname: Torvalds
fname: Linus
job : maintainer

Question:-
Why key of "litt" is printed first thought the first key of hash is
"torvalds"?

Thanks and regards,
Neela.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top