本文简要介绍ruby语言中  Array.delete  的用法。
用法
delete(obj) → deleted_objectdelete(obj) {|nosuch| ... } → deleted_object or block_return从 self 中删除零个或多个元素;返回 self 。
当没有给出块时,从 self 中删除每个元素 ele 使得 ele == obj ;返回最后删除的元素:
s1 = 'bar'; s2 = 'bar'
a = [:foo, s1, 2, s2]
a.delete('bar') # => "bar"
a # => [:foo, 2]如果没有删除任何元素,则返回 nil。
当给定一个块时,从 self 中删除每个元素 ele 使得 ele == obj 。
如果找到任何此类元素,则忽略该块并返回最后删除的元素:
s1 = 'bar'; s2 = 'bar'
a = [:foo, s1, 2, s2]
deleted_obj = a.delete('bar') {|obj| fail 'Cannot happen' }
a # => [:foo, 2]如果没有找到这样的元素,则返回块的返回值:
a = [:foo, 'bar', 2]
a.delete(:nosuch) {|obj| "#{obj} not found" } # => "nosuch not found"相关用法
- Ruby Array.delete_if用法及代码示例
- Ruby Array.delete_at用法及代码示例
- Ruby Array.drop_while用法及代码示例
- Ruby Array.drop用法及代码示例
- Ruby Array.dig用法及代码示例
- Ruby Array.difference用法及代码示例
- Ruby Array.push用法及代码示例
- Ruby Array.hash用法及代码示例
- Ruby Array.to_a用法及代码示例
- Ruby Array.to_h用法及代码示例
- Ruby Array.to_s用法及代码示例
- Ruby Array.array + other_array用法及代码示例
- Ruby Array.take()用法及代码示例
- Ruby Array.reject!用法及代码示例
- Ruby Array.flatten!用法及代码示例
- Ruby Array.reject用法及代码示例
- Ruby Array.repeated_permutation()用法及代码示例
- Ruby Array.index()用法及代码示例
- Ruby Array.pack()用法及代码示例
- Ruby Array.rassoc(obj)用法及代码示例
- Ruby Array.values_at()用法及代码示例
- Ruby Array.each用法及代码示例
- Ruby Array.fetch用法及代码示例
- Ruby Array.flatten用法及代码示例
- Ruby Array.sort用法及代码示例
注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 Array.delete。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
