yes, it’s the normal behaviour.
If you want you can initialize an Array with a block, say:
Array.new(10) { rand }
The block gets executed for every element in the array, giving
different obvjects (or many copies of the same, in your case)
···
il Tue, 11 May 2004 11:15:34 +0200, “E.-R. Bruecklmeier” unet01@radio-eriwan.de ha scritto::
Is it intentional that the new method fills the array with just one
instance of [0,0]? In my sense this is a violation of the PoLS.
Is it intentional that the new method fills the array with just one
instance of [0,0]? In my sense this is a violation of the PoLS.
Well, I’d say no, if you aware of how Ruby deals with instances and
variables. All variables hold references - there is no distinction
between pass by value and pass by reference as it is in C++. Then you’ll
see that a single instance is handed over to the constructor in your first
example. That instance is used for each array element, which is fine for
immutable classes (integers for example). If you need different
instances, you’ll need the block form as shown by you or even better the
other one: