From: "Jesús Gabriel y Galán" Date: 2011-01-04T04:00:28+09:00 Subject: Re: Different behaviour in irb and ruby On Mon, Jan 3, 2011 at 7:38 PM, Vincent V. wrote: > I'm new to Ruby and just finished my first program. > > # simple quiz program > questions={"Question 1"=>"Answer 1", >           "Question 2"=>"Answer 2", >           "Question 3"=>"Answer 3", >           "Question 4"=>"Answer 4", >           "Question 5"=>"Answer 5", >           "Question 6"=>"Answer 6", >           "Question 7"=>"Answer 7", >           "Question 8"=>"Answer 8", >           "Question 9"=>"Answer 9", >           "Question 10"=>"Answer 10"} > score=0 > i=0 > while i<10 >  puts questions.keys[i] >  if gets.chomp == questions.values[i] >    puts "Good answer !" >    score+=1 >  else >    puts "Bad answer !" >  end >  i+=1 > end > > puts "Your score is #{score}/10." > > When I past it in irb(0.9.5) it just runs fine, but when it runs with > ruby(1.8.7) or (1.9.2) the order of the questions changes. > > Can someone explain me what goes wrong (and why)? If you want to access the answers by question number, you should use an array and not a Hash. In Ruby 1.9 the Hash maintains the keys in insertion order, but this is not the case in Ruby 1.8, I think. I'm not sure why you find that behaviour in 1.9.2. In any case, I think you should use an Array. Jesus.