Array#drop_while() : drop_while() is a Array class method which drops elements from the array following the specific condition.
Syntax: Array.drop_while()
Parameter: block - condition to follow
Return: array after removing the elements based on the condition
Code #1 : Example for drop_while() method
a = [ 18 , 22 , 33 , 100 , 5 , 6 ]
b = [ 1 , 4 , 1 , 1 , 88 , 9 ]
puts "drop : #{a.drop_while {|x| x < 14}}\n\n"
puts "drop : #{b.drop_while{|x| x != 1}}\n\n"
|
Output :
drop : [18, 22, 33, 100]
drop : [4, 88, 9]
Code #2 : Example for drop_while() method
a = [ "abc" , "nil" , "dog" ]
b = [ "cow" , nil , "dog" ]
c = [ "cat" , nil , nil ]
puts "drop : #{a.drop_while {|x| x != " dog "}}\n\n"
puts "drop : #{b.drop_while{|x| x == " cow "}}\n\n"
|
Output :
drop : ["dog"]
drop : [nil, "dog"]
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!