In message "[ruby-talk:5652] Array#insert"
on 00/10/18, Aleksi Niemel<aleksi.niemela / cinnober.com> writes:
>I was looking up how Array#insert should be called. Uh, there's none! Maybe
>there's some other suitable method somewhere in the middle of the other, but
>I didn't spot it fast.
>
>Of course ary[index,0] = stuff. But it's very annoying format, when you
>could be more explicit too. Besides there's always the surprise of
>
> ruby -e'a=[1,2,3]; a[1,0]=[4,5]; p a;'
> [1, 4, 5, 2, 3]
> ruby -e'a=[1,2,3]; a[1,0]=[[4,5]]; p a;'
> [1, [4, 5], 2, 3]
>
>lurking, and I'd expect the first example output what the second does.
How about this?
class Array
def insert(pos, *obj)
self[pos,0] = obj
self
end
end
a = [1,2,3] ; a.insert(1, [4, 5]) ; p a #=> [1, [4, 5], 2, 3]
a = [1,2,3] ; a.insert(1, 4, 5 ) ; p a #=> [1, 4, 5, 2, 3]
-- gotoken