Problem with each_key

M

Marcin Górski

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.
 
T

Tim Hunter

Marcin said:
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.
 
M

Marcin Górski

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.
 
R

Rick DeNatale

2009/7/24 Marcin G=F3rski said:
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.


--=20
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
 
M

Marcin Górski

Rick said:
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?.
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top