Array#drop_while():drop_while()是一种Array类方法,可根据特定条件从数组中删除元素。
用法: Array.drop_while() 参数: block - condition to follow 返回: array after removing the elements based on the condition
代码#1:drop_while()方法示例
# 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"
输出:
drop:[18, 22, 33, 100] drop:[4, 88, 9]
代码2:drop_while()方法示例
# 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"
输出:
drop:["dog"] drop:[nil, "dog"]
相关用法
- Ruby Array class each()用法及代码示例
- Ruby Array class eql?()用法及代码示例
- Ruby Array class find_index()用法及代码示例
- Ruby Array class fill()用法及代码示例
- Ruby Array class fetch()用法及代码示例
- Ruby Array class empty?()用法及代码示例
- Ruby Array class each_index()用法及代码示例
- Ruby Array class frozen?()用法及代码示例
- Ruby Array class include?()用法及代码示例
- Ruby Array dig()用法及代码示例
- Ruby Array at()用法及代码示例
- Ruby Array any?()用法及代码示例
- Ruby Array map!()用法及代码示例
- Ruby Array concat()用法及代码示例
- Ruby Array intersection用法及代码示例
注:本文由纯净天空筛选整理自mayank5326大神的英文原创作品 Ruby | Array class drop_while() operation。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。