Home » Ruby programming

Array element assignment with ary[start,length] -> object in Ruby

Here, we are going to learn about the array element assignment with ary[start,length] -> object in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on January 05, 2020

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 Array_instance[index] operator? That was also one of the ways to assign elements to the Array instances because with the help of that method we were adding the element or object in the particular index of the Array instance and that index may be negative or positive. 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[start,length]

In this way of assignment, you will have to provide two indexes of the Array instance where you want to put your element or object of a particular class. You will have to provide two indexes means that one index will refer to the starting index and the second index will refer to the closing index. Got confused? Let me clear this with the help of a theoretical example if you are providing 1 and 3 as the index then the elements will be assigned at index 1,2 and 3. Now, let us understand the scenario or method with the help of syntax and examples which are provided below,

Syntax:

    array_instance[start,end] = object or object(s)

Example 1:

=begin
  Ruby program to demonstrate 
  Array_instance[start,end] = object
=end

# array declaration
array_instance = ["a","c","c","v","samir","Hrithik"]

# input the indexes
puts "Enter the start index you want to put element in:"
start = gets.chomp.to_i
puts "Enter the end index you want to put element in:"
endi = gets.chomp.to_i

if(endi<array_instance.count && start>0)
  for i in start..endi
    puts "Enter the element:"
    array_instance[i] = gets.chomp
  end
else
  puts "Index out of bound"
end

# printing the array 
puts "The final array is:"
print array_instance

Output

Enter the start index you want to put element in:
 1
Enter the end index you want to put element in:
 3
Enter the element:
 Hrithik
Enter the element:
 Shivang
Enter the element:
 Prem
The final array is:
["a", "Hrithik", "Shivang", "Prem", "samir", "Hrithik"]

Explanation:

In the above code, you can observe that we are asking the user for two indexes. If both the indexes can fulfill the "if" condition of our code, then further processing is taking place. With the help of "for" loop and range, we are asking the user for the elements. You can observe that our elements are reflected in the final Array after overwriting the previous elements assigned at the time of declaring Array instance.

Example 2:

=begin
  Ruby program to demonstrate 
  Array_instance[start,end] = object
=end

# array declaration
array_instance = ["a","c","c","v","samir","Hrithik"]

# input the indexes
puts "Enter the start index you want to put element in:"
start = gets.chomp.to_i
puts "Enter the end index you want to put element in:"
endi = gets.chomp.to_i
puts "Enter the element:"

# array instance
array_instance[start,endi] = gets.chomp

# printing the array
puts "The final array is:"
print array_instance

Output

Enter the start index you want to put element in:
 1
Enter the end index you want to put element in:
 3
Enter the element:
 ?
The final array is:
["a", "?", "samir", "Hrithik"]

Explanation:

In the above code, you can observe that we are taking input from the user for two indexes and we are demanding only a single element. This example is introduced just to show you that when you provide a single element to the range, it specifies the whole range a single element that can be accessed by the start index only.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.