-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.rb
29 lines (27 loc) · 801 Bytes
/
array.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Array
# Determine if two arrays have at least one intersecting member
#
# Takes an optional block argument which is used to specify how to compare
# the array pairs.
#
# @example Default comparison -- checks simple equality
# [1, 2, 3].in?([3, 4, 5]) # => true
# [1, 2, 3].in?([4, 5, 6]) # => false
#
# @example Comparison with block argument
# ['one', 'two'].in?(['on']) { |a, b| a =~ /#{b}/ } # => true
# ['one', 'two'].in?(['z']) { |a, b| a =~ /#{b}/ } # => false
#
# @param [Array] arr The array to match against
# @param [Proc] block An optional condition
# @return [Boolean]
def in?(arr, &block)
if block_given?
self.detect do |i|
arr.detect { |j| yield(i, j) }
end
else
!(self & arr).empty?
end
end
end