Home »
Ruby Tutorial
Array element assignment with ary[index] -> object in Ruby
By IncludeHelp Last updated : November 22, 2024
In the last article, we have learnt how we can add an object as an element to the object of Array class and we did that with the help of >> operator? That was also one of the ways to assign elements to the Array instances because with the help of >> operator or method we were pushing or adding the element or object in the last index of the Array instance. In this article, we will about one more way through which we can assign elements to the various indexes of the Array instance. At the end of the article, you will be aware of the method very clearly.
Let us see the syntax and implementation of the method with the help of examples.
Assigning element with ary[index]
In this way of assignment, you will have to provide the particular index of the Array instance where you want to put your element or object of a particular class. If you will provide a negative index, then it will start counting it from the end. Got confused? Let me explain the scenario with the help of theoretical example, if you are providing -1 as the index then it will assign the object to the first index of the Array instance from the last and if you pass -2 as the index, then it will assign the object to the second last index of the Array object. Now, let us understand the scenario or method with the help of syntax and examples which are provided below,
Syntax
array_instance[index] = object
Example 1
=begin
Ruby program to demonstrate
array_instance[index] = object
=end
# array declaration
array_instance = ["a","c","c","v","samir","Hrithik"]
# input the index and element
puts "Enter the index you want to put element in:"
ind = gets.chomp.to_i
if(ind<array_instance.count)
puts "Enter the element:"
array_instance[ind] = gets.chomp
else
puts "Index out of bound"
end
# printing the array
puts "The final array is:"
print array_instance
Output
Enter the index you want to put element in:
2
Enter the element:
Prem
The final array is:
["a", "c", "Prem", "v", "samir", "Hrithik"]
Explanation
In the above code, you can observe that we are asking the index from the user in which he wants to put his element or object. First, we are checking whether the index is feasible if it is more than or equal to the length of Array, we are notifying the user. If this is not the case then we are putting the element in that particular index. From the above example, you can observe that an object is already present in that particular index but we have overwritten it with the help of this method. So, we can conclude that this method can be used for overwriting objects also.
Example 2
=begin
Ruby program to demonstrate
array_instance[index] = object
=end
# array declaration
array_instance = ["a","c","c","v","samir","Hrithik"]
# input index and element
puts "Enter the negative index you want to put element in:"
ind = gets.chomp.to_i
puts "Enter the element:"
array_instance[ind] = gets.chomp
# printing the array
puts "The final array is:"
print array_instance
Output
Enter the negative index you want to put element in:
-2
Enter the element:
Prem
The final array is:
["a", "c", "c", "v", "Prem", "Hrithik"]
Explanation
In the above code, you can observe that we are taking input from the user for the negative index. You can see that when the user has entered -2, then we have printed it and found that our object is residing in the second last element of the Array instance. The method has overwritten the index from "samir" to "Prem".