Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

set
ruby
disjoint
communitycreator

What is set.disjoint?() in Ruby?

okwudili onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

In Ruby, the disjoint?()method is used on sets. A set is a collection of unordered unique elements. When this method is called, it returns a Boolean value. If two sets have no element in common then we say they are disjoint. A disjoint is the opposite of an intersection.

Syntax

set1.disjoint?(set2)
Syntax for the disjoint?() method of a set in Ruby

Parameters

set1 and set2: We want to check if set1 and set2 are disjoint.

Return value

The value returned is a Boolean. true is returned if the two sets have no element in common, that is they are disjoint. Otherwise, false is returned.

Example

# require set class
require "set"
# create some sets
set1 = Set.new([1, 2, 3])
set2 = Set.new([4..5])
set3 = Set.new([3, 4])
set4 = Set.new([4, 5])
# check if disjoint sets
puts set1.disjoint? set2 # true
puts set1.disjoint? set3 # false
puts set1.disjoint? set4 # true
Using the disjoint?() method on sets

Explanation

  • Line 2: We require the set class.
  • Lines 5–8: We create and initialize four sets for comparison purposes.
  • Lines 11–13: We check if the sets are disjoint using the disjoint? method and print the results to the console.

RELATED TAGS

set
ruby
disjoint
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring