Array#&()是一個Array類方法,它對數組執行集合相交操作。並返回兩個數組的共同點。
用法:Array.&()
參數:用於執行相交操作的數組。
返回:兩個數組中的公共元素。
範例1:
# Ruby code for &() method
# showing intersection operation
# declaration of array
a = [18, 22, 33, 4, 5, 6]
# declaration of array
b = [5, 4, 22, 1, 88, 9]
# declaration of array
c = [18, 22, 33, 40, 50, 6]
# a intersecting b
puts "intersection of a and b:#{a & b}\n\n"
# a intersecting c
puts "intersection of a and c:#{a & c}\n\n"
# b intersecting c
puts "intersection of b and c:#{b & c}\n\n"
輸出:
intersection of a and b:[22, 4, 5] intersection of a and c:[18, 22, 33, 6] intersection of b and c:[22]
範例2:
# Ruby code for &() method
# showing intersection operation
# declaration of array
a = ["abc", "xyz", "dog"]
# declaration of array
b = ["cow", "cat", "dog"]
# declaration of array
c = ["cat", "1", "dog"]
# a intersecting b
puts "intersection of a and b:#{a & b}\n\n"
# a intersecting c
puts "intersection of a and c:#{a & c}\n\n"
# b intersecting c
puts "intersection of b and c:#{b & c}\n\n"
輸出:
intersection of a and b:["dog"] intersection of a and c:["dog"] intersection of b and c:["cat", "dog"]
相關用法
- Ruby Array any?()用法及代碼示例
- Ruby Array at()用法及代碼示例
- Ruby Array map!()用法及代碼示例
- Ruby Array dig()用法及代碼示例
- Ruby Array class each()用法及代碼示例
- Ruby Array delete_if()用法及代碼示例
- Ruby Array delete()用法及代碼示例
- Ruby Array size()用法及代碼示例
- Ruby Array delete_at()用法及代碼示例
- Ruby Array collect()用法及代碼示例
- Ruby Array drop()用法及代碼示例
- Ruby Array cycle()用法及代碼示例
- Ruby Array compact()用法及代碼示例
- Ruby Array bsearch_index()用法及代碼示例
- Ruby Array clear()用法及代碼示例
注:本文由純淨天空篩選整理自mayank5326大神的英文原創作品 Ruby | Array intersection operation。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。