Problem with each_key

Hi,
I use NetBeans as IDE for Ruby. I tested my script on JRuby and got
result I expected. The problem concern Hash and each_key iterator. I
have diffrent result on Ruby 1.8.7.

Ruby 1.8.7:
irb(main):008:0> hash = { ‘a’ => 20, ‘b’ => 300, ‘k’ => 1 }
=> {“k”=>1, “a”=>20, “b”=>300}
irb(main):009:0> hash.each_key { |key| puts “key: #{key} #{hash[key]}” }
key: k 1
key: a 20
key: b 300
=> {“k”=>1, “a”=>20, “b”=>300}
irb(main):010:0>

In NetBeans on JRuby I wrote:
hash = { ‘a’ => 20, ‘b’ => 300, ‘k’ => 1 }
puts hash.each_key { |key| puts “key: #{key} #{hash[key]}” }

Output:
key: a 20
key: b 300
key: k 1
a20b300k1

I would like to have the same sequence as in JRuby. Which iterator
should I use? Thank you for help.

Marcin Górski wrote:

Hi,
I use NetBeans as IDE for Ruby. I tested my script on JRuby and got
result I expected. The problem concern Hash and each_key iterator. I
have diffrent result on Ruby 1.8.7.
(snip)

I would like to have the same sequence as in JRuby. Which iterator
should I use? Thank you for help.

I don’t think you’re going to get it. In 1.8.7 hashes are by definition
unordered and the actual order is implementation-specific.

I don’t think you’re going to get it. In 1.8.7 hashes are by definition
unordered and the actual order is implementation-specific.
Well I found information on JRuby 1.2 page:
“A 1.8.6 compatible Ruby interpreter written in 100% pure Java.”
I think that Ruby 1.8.7 and 1.8.6 shouldn’t differ very much.

Rick Denatale wrote:

2009/7/24 Marcin G�rski [email protected]:
This means that you can’t rely on a particular order, so even though
JRuby enumerates this particular hash in that order, and 1.8.7
enumerates in a different order, they both are compatible with the
documented behavior.

Thank you for your answers. I solved my problem without relying on
each_key using additional arrays, uniq and has_key?.

2009/7/24 Marcin Górski [email protected]:

I don’t think you’re going to get it. In 1.8.7 hashes are by definition
unordered and the actual order is implementation-specific.
Well I found information on JRuby 1.2 page:
“A 1.8.6 compatible Ruby interpreter written in 100% pure Java.”
I think that Ruby 1.8.7 and 1.8.6 shouldn’t differ very much.

Well they differ, although not in this regard. BUT

The point is than in Ruby < 1.9, there is no specified guarantee about
the order in which hashes enumerate their elements.

This means that you can’t rely on a particular order, so even though
JRuby enumerates this particular hash in that order, and 1.8.7
enumerates in a different order, they both are compatible with the
documented behavior.


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs