# Unexplainable array phenomenon

**URL:** <https://www.ruby-forum.com/t/unexplainable-array-phenomenon/122645>\
**Category:** Ruby\
**Created:** [December 10, 2007, 10:57am UTC](https://www.ruby-forum.com/t/unexplainable-array-phenomenon/122645 "2007-12-10T10:57:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![saladinmundi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@saladinmundi](https://www.ruby-forum.com/u/saladinmundi)\
**Post date:** [December 10, 2007, 10:57am UTC](https://www.ruby-forum.com/t/unexplainable-array-phenomenon/122645/1 "2007-12-10T10:57:10Z")

</div>

hey folks, perhaps I’m too overtired, but I’ve got an array problem  
where I can’t see my error

class Main  
$someArray=[]  
$someArraytmp=[]

def someMethod(elem,k)  
$someArraytmp[k] = elem  
if(k==0)  
$someArray.push($someArraytmp)  
$someArray.each {|e| p e}  
end  
end  
end

t= Main.new  
t.someMethod(“hello”, 0)  
t.someMethod(“bye”, 0)

From my opinion there must two arrays in the global (array)variable  
$someArray.  
The first array element should contain a String hello at the first  
position.

The second array element (in $someArray) should be a String bye also at  
the first position.

So:  
$someArray[0]  
-\> []  
-\> “hello”  
-\> []  
-\> “bye”

But after compiling it there is

$someArray[0]  
-\> []  
-\> “bye”  
-\> []  
-\> “bye”

can someone explain to me why this happens?

thanx sala

---

<div class="post-metadata">

**Author:** ![saladinmundi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@saladinmundi](https://www.ruby-forum.com/u/saladinmundi)\
**Post date:** [December 10, 2007, 11:35am UTC](https://www.ruby-forum.com/t/unexplainable-array-phenomenon/122645/2 "2007-12-10T11:35:51Z")

</div>

2007/12/10, Saladin M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> if(k==0)
> 
> ```
> 
> → []  
> → []  
> → “bye”
> 
> can someone explain to me why this happens?

This is a typical aliasing issue. You insert the same tmp array two  
times into the global one and just overwrite the first element.  
Basically the same what happens if you do

irb(main):018:0\> Array.new(2,[“bye”])  
=\> [[“bye”], [“bye”]]  
irb(main):019:0\> a=Array.new(2,[“bye”])  
=\> [[“bye”], [“bye”]]  
irb(main):020:0\> a.map {|o| o.object\_id}  
=\> [1073420070, 1073420070]  
irb(main):021:0\> a.map {|o| o.object\_id}.uniq  
=\> [1073420070]

There are a lot of ways to improve your code. Since your method and  
variable naming is generic there is no way to guess what you want to  
achieve. The easiest fix to the code you show is to reinitialize  
$someArraytmp=[] every time you pushed it into the other Array. But  
ultimately you should use instance and local variables.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![saladinmundi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d07c76/32.png) [@saladinmundi](https://www.ruby-forum.com/u/saladinmundi)\
**Post date:** [December 10, 2007, 11:43am UTC](https://www.ruby-forum.com/t/unexplainable-array-phenomenon/122645/3 "2007-12-10T11:43:38Z")

</div>

thank you very much, I should have some more hours of sleep 🙂
