Hi –
On Fri, 2 May 2008, Arun K. wrote:
expect since empty or nill is the array, arr*n shouldnt change it.
irb(main):005:0> a = []
the reason i ask this is…
n = 10
x += n
does a perfect x = nil + n => n initialization, which is good and
explainable.
You can’t add 10 to nil. You’ll get an error if you try to do that,
because nil has no + method.
In the array case however,
One would expect [atleast i am ] the elements of the source array [if
any] be appended to the target. Now why is the reference going in making it
a recursive :?
sorry if this question is lame or bozo but i feel a clear behaviour enables
clear understanding or would require even more clearer reasoning.
You’ve using the << method:
num_arr = Array.new
num_arr << num_arr
which appends the object num_arr (not its elements, but it, itself) to
the object num_arr.
If you want to add the elements of an array to another array, you can
use concat:
irb(main):001:0> a = [1,2,3]
=> [1, 2, 3]
irb(main):002:0> a.concat(a)
=> [1, 2, 3, 1, 2, 3]
It’s all perfectly reasonable – you just have to know what the
methods you’re using actually do
David