From: David Alan Black Date: 2002-01-25T23:44:35+09:00 Subject: Re: Serious Array Bug in Ruby 1.6.6? Hello -- On Fri, 25 Jan 2002, Bill Tj wrote: > Hi, > > I am sorry that I still don't see the reason why the array constructor > behaves the way it is. If I want to create an array of hashes, I get > the following options: > > a = Array.new (2, Hash.new) # DOES NOT WORK CURRENTLY > a = (1..2).collect {Hash.new} a = Array.new(2).map { Hash.new } > a = Array.fetch... # Still under discussion > > To me, because Ruby has been so easy and natural in every other > aspect, I would like the first option (which currently does not work) > to be the way. I have used Perl before, and creating arrays of hashes > and sending it to proc's have been complicated; we cannot just read > the first few chapters of a Perl book and be able to do it right. This is sort of a circular argument.... You've listed Array.new(2, Hash.new) as one of the options for creating an array of two different hashes, and then declared that the construct "does not work". But actually it does work precisely as documented. I know that your point is that you wish it behaved differently, but that's different from not working :-) > Ruby is different. After just learning a few basic things, even > creating complicated data structures is almost effortless. That is, > until I want to create an array of hashes. To me, using collect or > map or fetch requires the person to really understand many functions > available for array. (I have done that, but still missed the point.) > Therefore, can we just change the behavior of Array, so that > > a = Array.new (n, something) > > will create n copies of that something? If that something is a > numeric or a literal string, all n members refer to the same object. > But if that something is a container, at least, then Ruby should > create n different container objects. What counts as a container, though? class Thing def initialize @inner_container = [] end def []=(*args) @inner_container.[]=(*args) end def [](*i) @inner_container[*i] end # etc. end t = Thing.new t[3] = "hi" p t[3] # => "hi" a = Array.new(2, Thing.new) # Container? > I don't really know about array efficiency or whether even it is > possible. To me, it is just a natural way of programming. (I don't > remember what happens if I do the same thing in C++, whether n > different objects will be created; I guess it depends on the copy > constructor or the assignment operator.) My question is, if Array.new > (n, Hash.new) just creates a single hash, what is the purpose of it? I've never used it, but then again I don't think every possible statement based on a language's semantics has to have a purpose that's defineable in advance. You could put: x = x somewhere in a program, and it wouldn't serve any purpose, but that doesn't mean that the language's support for assignment to variables should be reconsidered. (Sort of a reductio ad absurdum, I admit, but still I think the principle might apply.) David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav