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.
Daniel L. (Guest)
on 2007-06-12 14: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.
Alin P. (Guest)
on 2007-06-12 14:55
Daniel L. 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
Farrel L. (Guest)
on 2007-09-26 01:04
(Received via mailing list)
On 12/06/07, Daniel L. <removed_email_address@domain.invalid> 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
Alex Y. (Guest)
on 2007-09-26 01:04
(Received via mailing list)
Daniel L. wrote:
>
a << 'foo'
or
a.push('foo')
Markus S. (Guest)
on 2007-09-26 01:04
(Received via mailing list)
On Tuesday 12 June 2007 11:20:48 Daniel L. wrote:
>
> but i'm almost sure Ruby has a better way to do that!?
>
> Thx for any help
> D.


Tray:

a << 'foo'
GrzechG (Guest)
on 2007-09-26 01: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
Stefano C. (Guest)
on 2007-09-26 01:10
(Received via mailing list)
Alle martedì 12 giugno 2007, Daniel L. 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.