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




RUBY - REPLACE ARRAY ELEMENTS


How to change/replace an Item in a Array?


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.


Example :



x = ["Mohan", "Kriti", "Salim"]
x[1] = "Paul"
puts x 


Output :



  Mohan
  Paul
  Salim

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

java_Collections

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"
java_Collections


And we get the below output,


Mohan
Paul
Salim

How to change/replace an Item in a Array with another Array?


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.


Example :



x = ["Mohan", "Kriti", "Salim"]
x[1] = ["Rishav","Priya"]
puts x 


Output :



  Mohan
  Rishav
  Priya
  Salim

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

java_Collections

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"]
java_Collections


And what happens is, Keshav gets replaced with with [Rishav, Priya].


And the new array looks like this,


['Mohan', ['Rishav', 'Priya'], 'Salim']

How to change/replace a range of Items in a Array?


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.


Example :



x = ["Mohan", "John", "Paul", "Kriti", "Salim"]
x[1,3] = ["Anwar", "Mitali", "Anjana"]
puts x 


Output :



  Mohan
  Anwar
  Mitali
  Anjana
  Salim

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,

java_Collections

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"]
java_Collections


And the Array with replaced values is,

java_Collections

And the new array is,


['Mohan', 'Anwar', 'Mitali', 'Anjana', 'Salim']