Forum: Ruby add element at end of array

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2ac62c781138715724898be538351f7a?d=identicon&s=25 Daniel Liebig (Guest)
on 2007-06-12 12:02
(Received via mailing list)
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.
93697354fea1f7bae30d3ab89917c4c3?d=identicon&s=25 Alin Popa (alin_popa)
on 2007-06-12 12:55
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
05be5d6610e2c3f1780aa0e39e902e93?d=identicon&s=25 Farrel Lifson (Guest)
on 2007-09-25 23:04
(Received via mailing list)
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
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (regularfry)
on 2007-09-25 23:04
(Received via mailing list)
Daniel Liebig wrote:
>
a << 'foo'
or
a.push('foo')
D547bd74deebf2145ade3a2ac2286ae0?d=identicon&s=25 Markus Schirp (Guest)
on 2007-09-25 23:04
(Received via mailing list)
On Tuesday 12 June 2007 11:20:48 Daniel Liebig wrote:
>
> but i'm almost sure Ruby has a better way to do that!?
>
> Thx for any help
> D.


Tray:

a << 'foo'
27d7bfff157df29cd13e4647b7db073d?d=identicon&s=25 GrzechG (Guest)
on 2007-09-25 23:05
(Received via mailing list)
this is short example :


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

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

Regards,
Grzegorz Golebiowski
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2007-09-25 23:10
(Received via mailing list)
Alle martedì 12 giugno 2007, Daniel Liebig ha scritto:
>
> 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
This topic is locked and can not be replied to.