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.
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
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
this is short example :
a = [1,2,3,4]
=> [1, 2, 3, 4]
a.push(5)
=> [1, 2, 3, 4, 5]
Regards,
Grzegorz Golebiowski
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-forum.com/\.