Ruby | Enumerable drop_while() function

The drop_while() of enumerable is an inbuilt method in Ruby returns an array containing the rest elements after dropping elements up to, but not including, the first element for which the block returns nil or false. In case no block is given it returns the enumerator instead.

Syntax: enu.drop_while { |obj| block }

Parameters: The function takes the block which is used to check the condition.

Return Value: It returns an array of rest elements.

Example 1:




# Ruby program for drop_while method in Enumerable
  
# Initialize
enu = (1..50)
  
# returns rest elements
enu.drop_while {|obj| obj < 48}


Output:

[48, 49, 50]

Example 2:




# Ruby program for drop_while method in Enumerable
  
# Initialize
enu = [7, 14, 10, 21]
  
# returns rest elements
enu.drop_while {|obj| obj % 7 == 0}


Output:

[10, 21]