Let us say, we have a Array that contains three names, Mohan, Kriti and Salim. And we want to replace the name Kriti with a new name Paul.
x = ["Mohan", "Kriti", "Salim"] x[1] = "Paul" puts x
So, in the above code we have created a Array and initialised to the variable x.
x = ["Mohan", "Kriti", "Salim"]
Now, let us see, how the values are positioned in the Array
Now, if we see the above diagram, Kriti resides at position/index 1. So, what we do is, just replace the position/index 1 (i.e. x[1]) with the new name Paul.
x[1] = "Paul"
And we get the below output,
Mohan Paul Salim
Let us take the same example, in which we have a Array that contains three names, Mohan, Kriti and Salim. And we want to replace the name Kriti with a Array with another Array that has two names, Rishav and Rishav.
x = ["Mohan", "Kriti", "Salim"] x[1] = ["Rishav","Priya"] puts x
So, in the above code we have created a Array and initialised to the variable x.
x = ["Mohan", "Kriti", "Salim"]
Now, let us see, how the values are positioned in the Array
Now, if we see the above diagram, Kriti resides at position/index 1. So, what we do is, just replace the position/index 1 (i.e. x[1]) with the new Array that has two names, Rishav and Priya.
x[1] = ["Rishav","Priya"]
And what happens is, Keshav gets replaced with with [Rishav, Priya].
And the new array looks like this,
['Mohan', ['Rishav', 'Priya'], 'Salim']
Let us say, we have a Array that contains five names, Mohan, John, Paul, Kriti and Salim. And this time we want to replace the names John, Paul and Kriti with a new names, Anwar, Mitali and Anjana.
x = ["Mohan", "John", "Paul", "Kriti", "Salim"] x[1,3] = ["Anwar", "Mitali", "Anjana"] puts x
So, in the above code we have created a Array and initialised to the variable x.
x = ["Mohan", "John", "Paul", "Kriti", "Salim"]
In the below way the values are positioned in the Array,
Now, if we see the above diagram, John resides at position/index 1, Paul in 2 and Kriti in position/index 3.
So, what we do is, specify the range, 1 to 3 in x[1,3] and assign new values, Anwar, Mitali and Anjana.
And, John, Paul and Kriti gets replaced with Anwar, Mitali and Anjana.
x[1,3] = ["Anwar", "Mitali", "Anjana"]
And the Array with replaced values is,
And the new array is,
['Mohan', 'Anwar', 'Mitali', 'Anjana', 'Salim']