Skip to main content

Ruby Set intersect method

In ruby programming language, intersect() Set method are use to check where set and given set at least one element are common or not. Syntax of this method as follows.

set.intersect?(otherset) -> boolean true or false

This method accepts a set and returns a Boolean value. This method returns a true when the set and the given set contain at least one element of common. Otherwise returning an false value.

Example of intersect?() Set method

# Example 1
require "set"

s = Set[2,9,6,4,8,5]

# Check if there is at least one element 
# of s in the parameter set
r1 = s.intersect?(Set[4,9])
r2 = s.intersect?(Set[3,7])

print(" s : ",s.to_a)
# Display result
print("\n r1 : ",r1)
print("\n r2 : ",r2)
Ruby set intersect method example 1
 s : [2, 9, 6, 4, 8, 5]
 r1 : true
 r2 : false


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment