# Beginner Question on Ruby

**URL:** <https://www.ruby-forum.com/t/beginner-question-on-ruby/136474>\
**Category:** Ruby\
**Created:** [May 2, 2008, 1:52pm UTC](https://www.ruby-forum.com/t/beginner-question-on-ruby/136474 "2008-05-02T13:52:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arunvoip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e8c25b/32.png) [@arunvoip](https://www.ruby-forum.com/u/arunvoip)\
**Post date:** [May 2, 2008, 1:52pm UTC](https://www.ruby-forum.com/t/beginner-question-on-ruby/136474/1 "2008-05-02T13:52:40Z")

</div>

Hi all,

I am beginning to learn ruby and cant seem to reason this behaviour. I  
expect since empty or nill is the array, arr\*n shouldnt change it.

Why is this behaviour so?

Code:

num\_arr = Array.new  
puts “Num Alpha: #{num\_arr} and size: #{num\_arr.size}”  
puts “Num Alpha: #{num\_arr \<\< num\_arr} and size: #{num\_arr.size}”  
num\_arr.insert(-1, num\_arr \<\< num\_arr)  
puts “Num Alpha: #{num\_arr} and size: #{num\_arr.size}”

Output:

Num Alpha: and size: 0  
Num Alpha: […] and size: 1  
Num Alpha: […][…][…][…][…][…][…][…][…] and size: 3

---

<div class="post-metadata">

**Author:** ![arunvoip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e8c25b/32.png) [@arunvoip](https://www.ruby-forum.com/u/arunvoip)\
**Post date:** [May 2, 2008, 1:57pm UTC](https://www.ruby-forum.com/t/beginner-question-on-ruby/136474/2 "2008-05-02T13:57:59Z")

</div>

Hi –

On Fri, 2 May 2008, Arun K. wrote:

> puts “Num Alpha: #{num\_arr} and size: #{num\_arr.size}”  
> puts “Num Alpha: #{num\_arr \<\< num\_arr} and size: #{num\_arr.size}”  
> num\_arr.insert(-1, num\_arr \<\< num\_arr)  
> puts “Num Alpha: #{num\_arr} and size: #{num\_arr.size}”
> 
> Output:
> 
> Num Alpha: and size: 0  
> Num Alpha: […] and size: 1  
> Num Alpha: […][…][…][…][…][…][…][…][…] and size: 3

You’re inserting an array into itself. Ruby represents that kind of  
recursive inclusion with dots.

irb(main):005:0\> a = []  
=\> []  
irb(main):006:0\> a \<\< a  
=\> [[…]]

David

---

<div class="post-metadata">

**Author:** ![arunvoip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e8c25b/32.png) [@arunvoip](https://www.ruby-forum.com/u/arunvoip)\
**Post date:** [May 2, 2008, 2:16pm UTC](https://www.ruby-forum.com/t/beginner-question-on-ruby/136474/3 "2008-05-02T14:16:50Z")

</div>

Hello David,

Thanks for the answer.

But would you know why it is so? Is there something syntactic or  
semantic  
issue that I am making,

the reason i ask this is…

n = 10  
x += n  
does a perfect x = nil + n =\> n initialization, which is good and  
explainable.

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.

Cheers  
Arun

---

<div class="post-metadata">

**Author:** ![arunvoip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e8c25b/32.png) [@arunvoip](https://www.ruby-forum.com/u/arunvoip)\
**Post date:** [May 2, 2008, 2:25pm UTC](https://www.ruby-forum.com/t/beginner-question-on-ruby/136474/4 "2008-05-02T14:25:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![arunvoip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e8c25b/32.png) [@arunvoip](https://www.ruby-forum.com/u/arunvoip)\
**Post date:** [May 2, 2008, 2:28pm UTC](https://www.ruby-forum.com/t/beginner-question-on-ruby/136474/5 "2008-05-02T14:28:39Z")

</div>

And now i do 🙂 thanks.
