8

I have a Ruby array [1, 4]. I want to insert another array [2, 3] in the middle so that it becomes [1, 2, 3, 4]. I can achieve that with [1, 4].insert(1, [2, 3]).flatten, but is there a better way to do this?

2
  • What exactly means “in the middle”? Given [1,1,4,4] input, should [2,3] be inserted where? Commented Jan 27, 2016 at 9:14
  • 1
    "Where" should be a variable. I want to be able to insert in the beginning or anywhere in the middle, but not at the end (which can be easily achieved with array1 + array2). Commented Jan 27, 2016 at 9:16

4 Answers 4

18

You could do it the following way.

[1,4].insert(1,*[2,3])

The insert() method handles multiple parameters. Therefore you can convert your array to parameters with the splat operator *.

Sign up to request clarification or add additional context in comments.

2 Comments

All answers I got are great, but I think yours are the easiest to read. Thanks!
It's perhaps worth noting that [1,4].insert(2,*[2,3]) #=> [1,4,2,3], which allows the elements of the second array to be inserted after the last element of the first array.
5

One form of the method Array#[]= takes two arguments, index and length. When the latter is zero and the rvalue is an array, the method inserts the elements of the rvalue into the receiver before the element at the given index (and returns the rvalue). Therefore, to insert the elements of:

b = [2,3]

into:

a = [1,4]

before the element at index 1 (4), we write:

a[1,0] = b
  #=> [2,3]
a #=> [1,2,3,4]

Note:

a=[1,4]
a[0,0] = [2,3]
a #=> [2,3,1,4]

a=[1,4]
a[2,0] = [2,3]
a #=> [1,4,2,3]

a=[1,4]
a[4,0] = [2,3]
a #=> [1,4,nil,nil,2,3]]

which is why the insertion location is before the given index.

1 Comment

This is a very concise way to insert another array's elements. Maybe you should add some explanation (and some whitespace).
3
def insert_array receiver, pos, other
  receiver.insert pos, *other
end

insert_array [1, 4], 1, [2, 3]
#⇒ [1, 2, 3, 4]

or, the above might be achieved by monkeypatching the Array class:

class Array
  def insert_array pos, other
    insert pos, *other
  end
end

I believe, this is short enough notation to have any additional syntax sugar. BTW, flattening the result is not a good idea, since it will corrupt an input arrays, already having arrays inside:

[1, [4,5]].insert 1, *[2,3]
#⇒ [1, 2, 3, [4,5]]

but:

[1, [4,5]].insert(1, [2,3]).flatten
#⇒ [1, 2, 3, 4, 5]

Comments

0

My option without array#insert method

array = [1,2,3,6,7,8]
new_array = [4,5]

array[0...array.size/2] + new_array + array[array.size/2..-1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.