add element at end of array

D

Daniel Liebig

Hi there,

i'm pretty new to Ruby. I searched 'Programming Ruby' and the online
documentation but couldn't find a solution. So please forgive me my
simple question ;).

I just want to add a new value / object to an existing array. Right now
i'm using

a[a.length] = 'foo'

but i'm almost sure Ruby has a better way to do that!?

Thx for any help
D.
 
S

Stefano Crocco

Alle marted=EC 12 giugno 2007, Daniel Liebig ha scritto:
Hi there,

i'm pretty new to Ruby. I searched 'Programming Ruby' and the online
documentation but couldn't find a solution. So please forgive me my
simple question ;).

I just want to add a new value / object to an existing array. Right now
i'm using

a[a.length] =3D 'foo'

but i'm almost sure Ruby has a better way to do that!?

Thx for any help
D.

a << 'foo'

or=20

a.push 'foo'

Stefano
 
A

Alex Young

Daniel said:
Hi there,

i'm pretty new to Ruby. I searched 'Programming Ruby' and the online
documentation but couldn't find a solution. So please forgive me my
simple question ;).

I just want to add a new value / object to an existing array. Right now
i'm using

a[a.length] = 'foo'
a << 'foo'
or
a.push('foo')
 
F

Farrel Lifson

I just want to add a new value / object to an existing array. Right now
i'm using

a[a.length] = 'foo'

but i'm almost sure Ruby has a better way to do that!?

a << 'foo'

Farrel
 
M

Markus Schirp

Hi there,

i'm pretty new to Ruby. I searched 'Programming Ruby' and the online
documentation but couldn't find a solution. So please forgive me my
simple question ;).

I just want to add a new value / object to an existing array. Right now
i'm using

a[a.length] = 'foo'

but i'm almost sure Ruby has a better way to do that!?

Thx for any help
D.


Tray:

a << 'foo'
 
A

Alin Popa

Daniel said:
Hi there,

i'm pretty new to Ruby. I searched 'Programming Ruby' and the online
documentation but couldn't find a solution. So please forgive me my
simple question ;).

I just want to add a new value / object to an existing array. Right now
i'm using

a[a.length] = 'foo'

but i'm almost sure Ruby has a better way to do that!?

Thx for any help
D.

Hi Daniel,

Yes, there is an easier way to do that:

a = [1,2,3]
a.push(4)

Best regards,

Alin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,261
Messages
2,571,308
Members
47,967
Latest member
pakasi

Latest Threads

Top