本文簡要介紹ruby語言中 Array.difference
的用法。
用法
difference(*other_arrays) → new_array
返回一個新數組,其中僅包含 self
中未在任何數組 other_arrays
中找到的元素;使用 eql?
比較項目;來自 self
的訂單被保留:
[0, 1, 1, 2, 1, 1, 3, 1, 1].difference([1]) # => [0, 2, 3]
[0, 1, 2, 3].difference([3, 0], [1, 3]) # => [2]
[0, 1, 2].difference([4]) # => [0, 1, 2]
如果沒有給出參數,則返回 self
的副本。
相關: Array#-
。
相關用法
- Ruby Array.dig用法及代碼示例
- Ruby Array.drop_while用法及代碼示例
- Ruby Array.delete_if用法及代碼示例
- Ruby Array.drop用法及代碼示例
- Ruby Array.delete用法及代碼示例
- Ruby Array.delete_at用法及代碼示例
- 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.difference。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。