Agree with Joel. Use the block form of the new method, or you’ll get
multiple references to the same object.
This piece of code I believe does the trick. (No fault handling
included,
and probably not the most elegant, perhaps since I’m quite new to Ruby.)
def make_array(*a)
a.flatten!
if a.size > 1
Array.new(a.shift){make_array(a)}
elsif a.size == 1
Array.new(a.shift)
end
end
A subtle improvement to the function, as I failed to remember that I
could
expand an array to multiple arguments again when calling the function
recursively:
def make_array(*a)
if a.size > 1
Array.new(a.shift){make_array(*a)}
elsif a.size == 1
Array.new(a.shift)
end
end
And what about the case of an empty argument list?
def make_array(*a)
case a.size
when 0
raise ArgumentError, “Need at least one dimension”
when 1
Array.new(a.shift)
else
Array.new(a.shift){make_array(*a)}
end
end