Inserting an element to array at a specified position

since ruby’s array doesn’t have index(POS, OBJ) like in python, nor
splice() like in perl, what is the most common way to insert an element
at a specified position?

···


dave

David Garamond wrote:

since ruby's array doesn't have index(POS, OBJ) like in python, nor

sorry, i meant insert() not index().

···

--
dave

Hi,

···

In message “inserting an element to array at a specified position” on 02/10/09, David Garamond davegaramond@icqmail.com writes:

since ruby’s array doesn’t have index(POS, OBJ) like in python, nor
splice() like in perl, what is the most common way to insert an element
at a specified position?

ary = [0,1,2]
ary[1,0] = “a” # => ary = [0,“a”,1,2]

						matz.

irb(main):001:0> foo=%w(a b c)
[“a”, “b”, “c”]
irb(main):002:0> foo[1…0]=‘d’
“d”
irb(main):003:0> foo
[“a”, “d”, “b”, “c”]

Ian

···

On Wed 09 Oct 2002 at 16:47:52 +0900, David Garamond wrote:

David Garamond wrote:

since ruby’s array doesn’t have index(POS, OBJ) like in python, nor

sorry, i meant insert() not index().


Ian Macdonald | “Be excellent to each other.” – Bill, or
ian@caliban.org | Ted, in Bill and Ted’s Excellent Adventure
>
>
>

Yukihiro Matsumoto wrote:

since ruby’s array doesn’t have index(POS, OBJ) like in python, nor
splice() like in perl, what is the most common way to insert an element
at a specified position?

ary = [0,1,2]
ary[1,0] = “a” # => ary = [0,“a”,1,2]

hm, that’s the ugliest thing i’ve seen in ruby so far. :slight_smile:

since ruby already has a rich set of methods for Array, including
convenience/“secondary” stuffs like flatten(), indexes(), or rassoc(),
wouldn’t it make some sense to add insert() too? just a suggestion. (i
could add the method myself, of course, but…)

···


dave

Hi,

ary = [0,1,2]
ary[1,0] = “a” # => ary = [0,“a”,1,2]

hm, that’s the ugliest thing i’ve seen in ruby so far. :slight_smile:

Then use insert (1.7.x only).

ary=[0,1,2]
ary.insert(1,“a”)

since ruby already has a rich set of methods for Array, including
convenience/“secondary” stuffs like flatten(), indexes(), or rassoc(),
wouldn’t it make some sense to add insert() too? just a suggestion. (i
could add the method myself, of course, but…)

I guess I’ve heard you long before you opened your mouth. :wink:

						matz.
···

In message “Re: inserting an element to array at a specified position” on 02/10/09, David Garamond davegaramond@icqmail.com writes: