Orion Hunter wrote:
> My next approach was to create the array, with a dafault length, and iterate
> over it assigning a new array to each element:
> 
>     my_arr = Array.new( 4 )
>     my_arr.each{ |i|
>         i = Array.new
>     }
> 
>     my_arr.each { |j|
>         puts j.type
>     }
> 
> I get the following output:
>     NilClass
>     NilClass
>     NilClass
>     NilClass
>     [nil, nil, nil, nil]

[...snip...]

> Conclusion: I understand why I can't pass Array.new as the default value in
> Array.new.  However, I do _not_ understand why the first iterative approach
> does not work.  I obviously know how to acquire the desired result, but that
> doesn't quench my curiousity as to why the other does not work.  Can I
> bother someone for some enlightenment?

The iteration with 'each' yields a value, which is a reference to the
current object inside the Array at whatever index one is currently at.
It does not yield a mutable reference or pointer that allows
modification of the Array. On the first iteration with each
  i == my_arr[0] # is true
but the 'i' variable is still an individual variable, with no link nor
knowledge of my_arr[0]. They just happen to point to the same object. I
think this constitutes "pass-by-value" semantics, while you are thinking
of "pass-by-reference" semantics.

What I would do in the above case, however, is:

  (1..4).collect{ Array.new }

which fits better with the model you have in mind. It creates a new
array inserting the value returned from the block at the corresponding
position. 
The variant collect! does it in-place, so perhaps

  my_arr = Array.new( 4 )
  my_arr.collect!{ Array.new }

would be faster (for larger values of 4, atleast :-)

HTH

-- 
(\[ Kent Dahl ]/)_    _~_    __[ http://www.stud.ntnu.no/~kentda/ ]___/~
 ))\_student_/((  \__d L b__/  NTNU - graduate engineering - 5. year  )
( \__\_?|?_/__/ ) _)Industrial economics and technological management(
 \____/_?_\____/ (____engineering.discipline_=_Computer::Technology___)