Ruby drop array method

In ruby programming language, drop() method of array are use to drop initial n elements at beginning of array and get the remaining elements. This method takes a parameter-positive integer that represents the number of drop elements in front of the array. Its syntax is as follows.

Syntax Array.drop(number)
Parameter Positive integer value
Return Returns the remaining elements of array

Ruby drop method example

See some examples for using the drop () method in this section. Which describes the working process of this method.

# Example 1

# Our array
record = [4,5,6,7,8,9,10]

# Drop 4 element
data = record.drop(4)

# Display array elements
print(" record : ",record)
print("\n data   : ",data)
Ruby drop method example 1
 record : [4, 5, 6, 7, 8, 9, 10]
 data   : [8, 9, 10]

In case drop() method parameter value is greater than length of array. Then this method returning an empty array.

# Example 2

# Our array
record = [1,2,3,4,5]

# Drop 4 element
a = record.drop(4)

# n more than array
b = record.drop(6)

# Display array elements
print(" record : ",record)
print("\n a   : ",a)
print("\n b   : ",b)
Ruby drop method example 2
 record : [1, 2, 3, 4, 5]
 a   : [5]
 b   : []


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