Open In App

Ruby | Enumerable drop_while() function

Last Updated : 05 Dec, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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]

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads