Science, Tech, Math › Computer Science How to Combine Arrays in Ruby Share Flipboard Email Print Milan_Jovic/Getty Images Computer Science Ruby Programming PHP Programming Perl Python Java Programming Javascript Programming Delphi Programming C & C++ Programming Visual Basic View More By Michael Morin Michael Morin Computer Science Expert Michael Morin is a computer programmer specializing in Linux and Ruby. He has 30 years of experience studying, teaching and using the programming language. Learn about our Editorial Process Updated on June 20, 2019 "What is the best way to combine arrays?" This question is quite vague and can mean a few different things. Concatenation Concatenation is to append one thing to another. For example, concatenating the arrays [1,2,3] and [4,5,6] will give you [1,2,3,4,5,6]. This can be done in a few ways in Ruby. The first is the plus operator. This will append one array to the end of another, creating a third array with the elements of both. Alternatively, use the concat method (the + operator and concat method are functionally equivalent). If you're doing a lot of these operations you may wish to avoid this. Object creation is not free, and every one of these operations creates a third array. If you want to modify an array in place, making it longer with new elements you can use the << operator. However, if you try something like this, you'll get an unexpected result. Instead of the expected [1,2,3,4,5,6] array we get [1,2,3,[4,5,6]]. This makes sense, the append operator takes the object you give it and appends it to the end of the array. It didn't know or care that you tried to append another array to the array. So we can loop over it ourselves. Set Operations The world "combine" can also be used to describe the set operations. The basic set operations of intersection, union, and difference are available in Ruby. Remember that "sets" describe a set of objects (or in mathematics, numbers) that are unique in that set. For example, if you were to do a set operation on the array [1,1,2,3] Ruby will filter out that second 1, even though 1 may be in the resulting set. So be aware that these set operations are different than list operations. Sets and lists are fundamentally different things. You can take the union of two sets using the | operator. This is the "or" operator, if an element is in one set or the other, it's in the resulting set. So the result of [1,2,3] | [3,4,5] is [1,2,3,4,5] (remember that even though there are two threes, this is a set operation, not a list operation). The intersection of two sets is another way to combine two sets. Instead of an "or" operation, the intersection of two sets is an "and" operation. The elements of the resultant set are those in both sets. And, being an "and" operation, we use the & operator. So the result of [1,2,3] & [3,4,5] is simply [3]. Finally, another way to "combine" two sets is to take their difference. The difference of two sets is the set of all objects in the first set that is not in the second set. So [1,2,3] - [3,4,5] is [1,2]. Zipping Finally, there is "zipping." Two arrays can be zipped together combining them in a rather unique way. It's best to just show it first, and explain after. The result of [1,2,3].zip([3,4,5]) is [ [1,3], [2,4], [3,5] ]. So what happened here? The two arrays were combined, the first element being a list of all elements in the first position of both arrays. Zipping is a bit of a strange operation and you may not find much use for it. Its purpose is to combine two arrays whose elements closely correlate. Cite this Article Format mla apa chicago Your Citation Morin, Michael. "How to Combine Arrays in Ruby." ThoughtCo, Apr. 5, 2023, thoughtco.com/combining-arrays-in-ruby-2907842. Morin, Michael. (2023, April 5). How to Combine Arrays in Ruby. Retrieved from https://www.thoughtco.com/combining-arrays-in-ruby-2907842 Morin, Michael. "How to Combine Arrays in Ruby." ThoughtCo. https://www.thoughtco.com/combining-arrays-in-ruby-2907842 (accessed June 10, 2023). copy citation