Ruby slice array method

In ruby programming language, slice() array method are used to get particular element and subarray in array. Syntax of this method as follows.

Array.slice(index) → return object or nil
Array.slice(start, length) → return new array or nil
Array.slice(range) → return new array or nil

# Same as
Array[index] → return object or nil
Array[start, length] → return new array or nil
Array[range] → return new array or nil

Ruby Array.slice(index) method example

This method are used to get single index element from array. In case index element not exist so this method are returning nil value. If index present element then this method return value or reference of index position. Let see an example.

# Our array
arr  = [3, 7, [1,4,3]]

# Get single element
v1  = arr.slice(1)
v2  = arr.slice(2)
v3  = arr.slice(5)

# Display result
print(" v1 : ",v1)
print("\n v2 : ",v2)
print("\n v3 : ",v3)

# Or 
# Get single element
v4  = arr[1]
v5  = arr[2]
v6  = arr[5]

# Display result
print("\n v4 : ",v4)
print("\n v5 : ",v5)
print("\n v6 : ",v6)
Ruby slice method example 1
 v1 : 7
 v2 : [1, 4, 3]
 v3 : 
 v4 : 7
 v5 : [1, 4, 3]
 v6 : 

Ruby Array.slice(start, length) method example

In this method first parameter indicates starting index and second parameters indicate number of elements. When the first parameter is a positive value (accepts zero). Then elements are selected from left to right of the given index.

# Our array
arr  = [2, 3, 7, [1,4,3], 10]

# slice operation
v1  = arr.slice(1,2)
v2  = arr.slice(-2,2)
v3  = arr.slice(2,3)

# Display result
print(" Array : ",arr)
print("\n v1 : ",v1)
print("\n v2 : ",v2)
print("\n v3 : ",v3)
Ruby slice method example 2
 Array : [2, 3, 7, [1, 4, 3], 10]
 v1 : [3, 7]
 v2 : [[1, 4, 3], 10]
 v3 : [7, [1, 4, 3], 10]

When parameter values are not valid, Then result is based on situation. If parameter start index in valid and length is greater than number of remaining elements. Then this method are return all elements from start index to last.

When the parameter is positive and the first parameter is greater than the size of the array. Then this method returns an empty array.

When parameter value is negative then this method are returning a nil value. Let see an example to consider all situation.

 # Our array
arr  = [1,5,6,7,2,4]

# When start index valid and length greater than 0
v1  = arr.slice(5,1)
v2  = arr.slice(-5,2)

# Outside the range
v3  = arr.slice(6,7)
v4 = arr.slice(-1,-1)

# Display result
print(" Array : ",arr)
print("\n v1 : ",v1)
print("\n v2 : ",v2)
print("\n v3 : ",v3)
print("\n v4 : ",v4)
Ruby slice method example 3
 Array : [1, 5, 6, 7, 2, 4]
 v1 : [4]
 v2 : [5, 6]
 v3 : []
 v4 : 

Array.slice(start, length) and Array[start, length] are same in ruby programming.

Ruby Array.slice(range) method example

This method are returning elements in specified range. Let see an example.

 # Our array
arr  = [1,5,6,7,2,4,8]

# slice with range
v1  = arr.slice(1..4)
v2 = arr[2..3]
v3 = arr[-4..-2]

# Display result
print(" Array : ",arr)
print("\n v1 : ",v1)
print("\n v2 : ",v2)
print("\n v3 : ",v3)
Ruby slice method example 4
 Array : [1, 5, 6, 7, 2, 4, 8]
 v1 : [5, 6, 7, 2]
 v2 : [6, 7]
 v3 : [7, 2, 4]


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved