3

I have an array arr = [[1,2],[3,4]] and a column col = [5,6]

Is there an easy way to get an output of [[1,2,5],[3,4,6]] without looping? Thanks

1 Answer 1

5

Yes, using Array#transpose as follows:

arr = [[1,2],[3,4]]
col = [5,6]
pp (arr.transpose << col).transpose # => [[1, 2, 5], [3, 4, 6]]
Sign up to request clarification or add additional context in comments.

2 Comments

@mechanicalfish Just wanted to let you know the comment you responded to was deleted in case you wanted to delete yours. :-)
What about inserting the new column in to the first index?

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.