Home » Ruby programming

Array.slice!() Method with Example in Ruby

Ruby Array.slice!() Method: Here, we are going to learn about the Array.slice!() method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on February 09, 2020

Array.slice!() Method

In this article, we will study about Array.slice!() method. You all must be thinking the method must be doing something which is related to slicing of elements or objects in the Array instance. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.

Method description:

This method is a public instance method and defined for the Array class in Ruby's library. This method works on element reference and returns the element at the index which is passed with the method invocation. If you are passing two parameters with the method and those parameters are the start and the length then the method will return a subarray which will contain the elements from the start index and till the length index. This method will return subarray in the case when the range is passed as the parameter at the time of method invocation. This method is one of the examples of destructive method where the method brings changes in the actual arrangement of objects in the self Array.

Syntax:

    array_instance.slice!(index) -> object or nil
    or
    array_instance.slice!(start,length)-> new_array or nil
    or
    array_instance.slice!(range)-> new_array or nil

Argument(s) required:

You can provide a single index or range or start and length as the argument inside this method at the time of the method call. You will get the output on the basis of the argument you pass inside the method.

Example 1:

=begin
  Ruby program to demonstrate slice! method
=end

# array declaration
table = [2,4,6,8,10,12,14,16,18,20]

puts "Array slice! implementation"

puts "Enter the index you want to slice"
ind = gets.chomp.to_i

if(table.slice!(ind))
	puts "The element which is sliced is #{table.slice!(ind)}"
else
	puts "Array index out of bound"
end

puts "Array instance after slicing: #{table}"

Output

Array slice! implementation
Enter the index you want to slice
 4
The element which is sliced is 12
Array instance after slicing: [2, 4, 6, 8, 14, 16, 18, 20]

Explanation:

In the above code, you can observe that we are slicing the element from the Array instance with the help of Array.slice!() method. We are slicing it with the help of an index for which we have asked the user to the input value. The 4th index object has been sliced from the Array instance. The method is bringing changes in the self Array due to the fact that this method is one of the examples of destructive methods.

Example 2:

=begin
  Ruby program to demonstrate slice! method
=end

# array declaration
table = [2,4,6,8,10,12,14,16,18,20]

puts "Array slice! implementation"

puts "Enter the start index you want to slice"
ind = gets.chomp.to_i

puts "Enter the length"
len = gets.chomp.to_i

if(table.slice(ind,len))
	puts "The sub array which is sliced is #{table.slice!(ind,len)}"
else
	puts "Array index out of bound"
end

puts "Array instance after slicing: #{table}"

Output

Array slice! implementation
Enter the start index you want to slice
 3
Enter the length
 5
The sub array which is sliced is [8, 10, 12, 14, 16]
Array instance after slicing: [2, 4, 6, 18, 20]

Explanation:

In the above code you can observe that we are creating a subarray from the elements of the Array instance with the help of Array.slice!() method. We are slicing it with the help of two parameters which are namely start index and length for which we have asked the user to input values. In the output, you can see that the Array instance has been sliced from the 3rd index and to the 7th index resulting in the formation of an Array which contains five objects. The method is bringing changes in the self Array due to the fact that this method is one of the examples of destructive method.

Example 3:

=begin
  Ruby program to demonstrate slice! method
=end

# array declaration
table = [2,4,6,8,10,12,14,16,18,20]

puts "Array slice! implementation"

puts "Enter the start index you want to slice"
ind = gets.chomp.to_i

puts "Enter the end index you want to slice"
len = gets.chomp.to_i

if(table.slice(ind..len))
	puts "The sub array which is sliced is #{table.slice!(ind..len)}"
else
	puts "Array index out of bound"
end

puts "Array instance after slicing: #{table}"

Output

Array slice! implementation
Enter the start index you want to slice
 3
Enter the end index you want to slice
 5
The sub array which is sliced is [8, 10, 12]
Array instance after slicing: [2, 4, 6, 14, 16, 18, 20]

Explanation:

In the above code, you can observe that we are slicing a subarray from the Array instance. We are slicing it with the help of a range for which we have asked the user to input values. In the output, you can see that the Array instance has been sliced from the 3rd index and to the 5th index resulting in the formation of an Array which contains three objects. The method is bringing changes in the self Array due to the fact that this method is one of the examples of the destructive method.



Comments and Discussions!

Load comments ↻





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