From: William Djaja Tjokroaminata Date: 2002-01-25T01:56:24+09:00 Subject: Serious Array Bug in Ruby 1.6.6? Hi, I am currently using Ruby 1.6.6 on Linux Mandrake 8.1. I want to create an array of hashes, so according to an example in the "Programming Ruby" book on the new method for class array (http://www.rubycentral.com/book/ref_c_array.html#Array.new), I wrote a = Array.new (2, Hash.new) a[0]['key'] = 'value0' a[1]['key'] = 'value1' puts a[0]['key'] puts a[1]['key'] Of course (?), this gives value1 value1 On the other hand, the following code works: a = Array.new (2) for i in 0...a.length a[i] = Hash.new end a[0]['key'] = 'value0' a[1]['key'] = 'value1' puts a[0]['key'] puts a[1]['key'] which gives value0 value1 Definitely the book has said "...that is, anInteger references to the same anObject". Of course, after reading this, everything is obvious. However, it took me several hours to figure this out until I read the actual description. At least, probably the book should not give that example. In the more general terms, probably Ruby should change the behavior of Array.new (n, Hash.new) as opposite to, say, Array.new (n, "A"). By having all the members refer to the same hash object, Ruby has violated the Priciple of Least Surprise. Yes, probably it is more consistent in the way Ruby behaves currently. However, when a person creates an array of hashes, I don't think there is anybody who wants all the hashes to refer to the same thing, because it is useless. Regards, Bill