💭

Ruby で一方の配列にもう一方の配列の値が含まれているかどうかを判定する方法

2022/02/21に公開

TL;DR

array_a = [1,2,3]
array_b = [3,4,5]
array_c = [7]

array_a & array_b
# => [3]
(array_a & array_b).any?
# => true

array_a & array_c
# => []
(array_a & array_c).any?
# => false

Sample Method (Rails)

# Example: User.rb
def belongs_to_same_group_with?(other)
  (self.groups & other.groups).present?
end

👋

Discussion