# Array reference?

**URL:** <https://rubytalk.org/t/array-reference/225>\
**Category:** ruby-talk\
**Created:** [12 June 2002 11:01 UTC](https://rubytalk.org/t/array-reference/225 "2002-06-12T11:01:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hugo\_de\_Paix\_de\_Coeu](https://avatars.discourse-cdn.com/v4/letter/h/cdc98d/32.png) [@Hugo\_de\_Paix\_de\_Coeu](https://rubytalk.org/u/Hugo_de_Paix_de_Coeu)\
**Post date:** [12 June 2002 11:01 UTC](https://rubytalk.org/t/array-reference/225/1 "2002-06-12T11:01:46Z")

</div>

Hi,  
consider this bunk of code:

```
irb(main):001:0> def modify(a)
irb(main):002:1> a += [2]
irb(main):003:1> end
nil
irb(main):004:0> a = [1]
[1]
irb(main):005:0> modify(a)
[1, 2]
irb(main):006:0> a
[1] <<<< a hasn't been modified
irb(main):007:0>

```

Why “a” isn’t modified by “modify” function?, because this work with a  
Hash class:

```
irb(main):013:0> def modify(h)
irb(main):014:1> h[2] = 2
irb(main):015:1> end
nil
irb(main):016:0> h = { 1 => 1 }
{1=>1}
irb(main):017:0> modify(h)
2
irb(main):018:0> h
{1=>1, 2=>2} <<<< h has been modified

```

How can i make it working with an array?  
thanks …

---

<div class="post-metadata">

**Author:** ![Nikodemus\_Siivola](https://avatars.discourse-cdn.com/v4/letter/n/ba8739/32.png) [@Nikodemus\_Siivola](https://rubytalk.org/u/Nikodemus_Siivola)\
**Post date:** [12 June 2002 11:07 UTC](https://rubytalk.org/t/array-reference/225/2 "2002-06-12T11:07:58Z")

</div>

a \<\< 2

```
or the equivalent a.push( 2 )

```

– Nikodemus

> **···**
>
> On Wed, 12 Jun 2002, Hugo de Paix de Coeur wrote:
> 
> > irb(main):001:0\> def modify(a)  
> > irb(main):002:1\> a += [2]  
> > irb(main):003:1\> end

---

<div class="post-metadata">

**Author:** ![Alia\_Amin](https://avatars.discourse-cdn.com/v4/letter/a/4bbf92/32.png) [@Alia\_Amin](https://rubytalk.org/u/Alia_Amin)\
**Post date:** [12 June 2002 11:19 UTC](https://rubytalk.org/t/array-reference/225/3 "2002-06-12T11:19:34Z")

</div>

hello,

irb(main):007:0\> def modify(a)  
irb(main):008:1\> a += [2]  
irb(main):009:1\> return a  
irb(main):010:1\> end  
nil  
irb(main):011:0\> modify(a)  
[1, 2]  
irb(main):012:0\> a  
[1]  
irb(main):013:0\> a = modify(a)  
[1, 2]  
irb(main):014:0\> a  
[1, 2]

hth,

Alia

Hugo de Paix de Coeur wrote:

> **···**
>
> > Hi,  
> > consider this bunk of code:
> > 
> > ```
> > irb(main):001:0> def modify(a)
> > irb(main):002:1> a += [2]
> > irb(main):003:1> end
> > nil
> > irb(main):004:0> a = [1]
> > [1]
> > irb(main):005:0> modify(a)
> > [1, 2]
> > irb(main):006:0> a
> > [1] <<<< a hasn't been modified
> > irb(main):007:0>
> > 
> > ```
> > 
> > Why “a” isn’t modified by “modify” function?, because this work with a  
> > Hash class:
> > 
> > ```
> > irb(main):013:0> def modify(h)
> > irb(main):014:1> h[2] = 2
> > irb(main):015:1> end
> > nil
> > irb(main):016:0> h = { 1 => 1 }
> > {1=>1}
> > irb(main):017:0> modify(h)
> > 2
> > irb(main):018:0> h
> > {1=>1, 2=>2} <<<< h has been modified
> > 
> > ```
> > 
> > How can i make it working with an array?  
> > thanks …

---

<div class="post-metadata">

**Author:** ![Markus\_Jais3](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@Markus\_Jais3](https://rubytalk.org/u/Markus_Jais3)\
**Post date:** [12 June 2002 11:41 UTC](https://rubytalk.org/t/array-reference/225/4 "2002-06-12T11:41:50Z")

</div>

Hugo de Paix de Coeur wrote:

> Hi,  
> consider this bunk of code:
> 
> irb(main):001:0\> def modify(a)  
> irb(main):002:1\> a += [2]  
> irb(main):003:1\> end  
> nil  
> irb(main):004:0\> a = [1]  
> [1]  
> irb(main):005:0\> modify(a)  
> [1, 2]  
> irb(main):006:0\> a  
> [1] \<\<\<\< a hasn’t been modified  
> irb(main):007:0\>
> 
> Why “a” isn’t modified by “modify” function?, because this work with a  
> Hash class:
> 
> irb(main):013:0\> def modify(h)  
> irb(main):014:1\> h[2] = 2  
> irb(main):015:1\> end  
> nil  
> irb(main):016:0\> h = { 1 =\> 1 }  
> {1=\>1}  
> irb(main):017:0\> modify(h)  
> 2  
> irb(main):018:0\> h  
> {1=\>1, 2=\>2} \<\<\<\< h has been modified
> 
> How can i make it working with an array?  
> thanks …

hello  
I am not sure what you want.  
with the hash modify you use the assignment operator  
and with the array modify you use +=  
if you want to add another alement to array a  
you can write

def modify(a)  
a \<\< [2]  
end

this append the elemnt to the array. if you want to modify  
a special element, write  
a[index] = value

maybe I misunderstood you and this is a stupid answer.

markus

---

<div class="post-metadata">

**Author:** ![Jean-Hugues\_ROBERT](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@Jean-Hugues\_ROBERT](https://rubytalk.org/u/Jean-Hugues_ROBERT)\
**Post date:** [12 June 2002 14:58 UTC](https://rubytalk.org/t/array-reference/225/5 "2002-06-12T14:58:22Z")

</div>

a \<\< [2] I guess.  
a += [2] is equivalent to a = a + [2]. This changes a, not the array.

Jean-Hugues

> **···**
>
> At 20:01 12/06/2002 +0900, you wrote:
> 
> > Hi,  
> > consider this bunk of code:
> > 
> > ```
> > irb(main):001:0> def modify(a)
> > irb(main):002:1> a += [2]
> > irb(main):003:1> end
> > nil
> > irb(main):004:0> a = [1]
> > [1]
> > irb(main):005:0> modify(a)
> > [1, 2]
> > irb(main):006:0> a
> > [1] <<<< a hasn't been modified
> > irb(main):007:0>
> > 
> > ```
> > 
> > Why “a” isn’t modified by “modify” function?, because this work with a  
> > Hash class:
> > 
> > ```
> > irb(main):013:0> def modify(h)
> > irb(main):014:1> h[2] = 2
> > irb(main):015:1> end
> > nil
> > irb(main):016:0> h = { 1 => 1 }
> > {1=>1}
> > irb(main):017:0> modify(h)
> > 2
> > irb(main):018:0> h
> > {1=>1, 2=>2} <<<< h has been modified
> > 
> > ```
> > 
> > How can i make it working with an array?  
> > thanks …
> 
> * * *
> 
> Web: [@jhr is virteal, virtually real](http://hdl.handle.net/1030.37/1.1)  
> Phone: +33 (0) 4 92 27 74 17

---

<div class="post-metadata">

**Author:** ![Christoph1](https://avatars.discourse-cdn.com/v4/letter/c/85f322/32.png) [@Christoph1](https://rubytalk.org/u/Christoph1)\
**Post date:** [12 June 2002 23:43 UTC](https://rubytalk.org/t/array-reference/225/6 "2002-06-12T23:43:24Z")

</div>

“Hugo de Paix de Coeur” [kabhal@wanadoo.fr](mailto:kabhal@wanadoo.fr) wrote in  
…

> **···**
>
> * * *
> 
> def modify(a)  
> a.concat [2,3]  
> end
> 
> ## p a =[1],[a.id](http://a.id) modify(a) p a,[a.id](http://a.id)
> 
> ## [1] 20729280 [1, 2, 3] 20729280
> 
> /Christoph
