Skip to content
Related Articles

Related Articles

Ruby | Enumerable drop_while() function

Improve Article
Save Article
  • Last Updated : 05 Dec, 2019
Improve Article
Save Article

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]
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!