Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




RUBY - JOIN TWO ARRAYS


How to extend an existing Array or join two Arrays?


Let us say, we have a Array that contains three names, Mohan, Kriti and Salim. And we want to insert a new Array with two names Sia and Andrew at the end of the Array.


Well! Two arrays can be joined by using the + operator


Let us see with the below example.


Example :



x = ["Mohan", "Kriti", "Salim"]
y = ["Sia", "Andrew"]
z = x + y
puts z 


Output :



  Mohan
  Kriti
  Salim
  Sia
  Andrew

So, in the above code we have created a Array and initialised to the variable x.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the Array,

java_Collections

Also we have another Array that contains, Sia and Andrew.


	y = ["Sia", "Andrew"]
java_Collections


Next, we have used the + operator to add the the Arrays x and y.


z = x + y

And the Arrays x and yis joined and initialised to a new Array z.

java_Collections

And the new array z looks like,


['Mohan', 'Kriti', 'Salim', 'Sia', 'Andrew']