Add new value to top of an Array

Hi, Does anyone know how to add a new value to the top of an array,
instead of the bottom. eg. When using <<, this appends to the end.

On Tue, Jan 25, 2011 at 12:10 PM, Sam T. [email protected]
wrote:

Hi, Does anyone know how to add a new value to the top of an array,
instead of the bottom. eg. When using <<, this appends to the end.


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

a = [ “b”, “c”, “d” ]

a.unshift(“a”) => [“a”, “b”, “c”, “d”]

SOLVED: Cheers…

ruby-1.9.2-p136 :009 > x = [2,3,4]
=> [2, 3, 4]
ruby-1.9.2-p136 :010 > x.unshift(5)
=> [5, 2, 3, 4]

On Tue, Jan 25, 2011 at 7:29 PM, Sam T. [email protected]
wrote:

SOLVED: Cheers…

note, there is also Array#insert.
i usually use it since

a. i can insert *anywhere on the array w autofill features,
b. a similar method of the same name also works on Strings
c. i just need to remember one command :wink:

btw, others may not think that “top” of array is first element :wink:

best regards -botp

On Tuesday, January 25, 2011, Sam T. [email protected] wrote:

Hi, Does anyone know how to add a new value to the top of an array,
instead of the bottom. eg. When using <<, this appends to the end.

Array#unshift