Related Articles

Related Articles

Ruby | Array class drop_while() operation
  • Last Updated : 08 Jan, 2020

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for drop_while() method
  
# declaring array
a = [18, 22, 33, 100, 5, 6]
  
# declaring array
b = [1, 4, 1, 1, 88, 9]
  
# drop
puts "drop : #{a.drop_while {|x| x < 14}}\n\n"
  
# drop
puts "drop : #{b.drop_while{|x| x != 1}}\n\n"

chevron_right


Output :

drop : [18, 22, 33, 100]

drop : [4, 88, 9]

Code #2 : Example for drop_while() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for drop_while() method
  
# declaring array
a = ["abc", "nil", "dog"]
  
# declaring array
b = ["cow", nil, "dog"]
  
# declaring array
c = ["cat", nil, nil]
  
# drop
puts "drop : #{a.drop_while {|x| x != "dog"}}\n\n"
  
# drop
puts "drop : #{b.drop_while{|x| x == "cow"}}\n\n"

chevron_right


Output :

drop : ["dog"]

drop : [nil, "dog"]
My Personal Notes arrow_drop_up
Recommended Articles
Page :