[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

add element at end of array

Daniel Liebig

6/12/2007 9:18:00 AM

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.
6 Answers

Stefano Crocco

6/12/2007 9:26:00 AM

0

Alle martedì 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] = 'foo'
>
> but i'm almost sure Ruby has a better way to do that!?
>
> Thx for any help
> D.

a << 'foo'

or

a.push 'foo'

Stefano

Alex Young

6/12/2007 9:27:00 AM

0

Daniel Liebig wrote:
> 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')

--
Alex

Farrel Lifson

6/12/2007 9:28:00 AM

0

On 12/06/07, Daniel Liebig <daniel.liebig@wevin.de> wrote:
> 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

GrzechG

6/12/2007 9:29:00 AM

0

this is short example :


a = [1,2,3,4]
=> [1, 2, 3, 4]

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

Regards,
Grzegorz Golebiowski




Markus Schirp

6/12/2007 9:34:00 AM

0

On Tuesday 12 June 2007 11:20:48 Daniel Liebig wrote:
> 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'

Alin Popa

6/12/2007 10:56:00 AM

0

Daniel Liebig wrote:
> 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

--
Posted via http://www.ruby-....