Array reference?


(Hugo de Paix de Coeur) #1

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 …


(Nikodemus Siivola) #2

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


(Alia Amin) #3

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 …


(Markus Jais) #4

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


(Jean-Hugues ROBERT) #5

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: http://hdl.handle.net/1030.37/1.1
Phone: +33 (0) 4 92 27 74 17


(Christoph) #6

“Hugo de Paix de Coeur” kabhal@wanadoo.fr wrote in

···

def modify(a)
a.concat [2,3]
end

p a =[1],a.id
modify(a)
p a,a.id

[1]
20729280
[1, 2, 3]
20729280

/Christoph