Skip to content
Related Articles

Related Articles

Ruby | Set divide() function
  • Last Updated : 07 Jan, 2020

The divide() is an inbuilt method in Ruby returns a set of sets. It is divided according to the condition that is given by the blocks. In case they donot satisfy the given condition, they are divided into single elements.

Syntax: s1.divide(condition)

Parameters: The function takes the condition on which the set is to be divided into sets of set.

Return Value: It returns a new set built by duplicating the set, removing every element that appears in the given enumerable object.

Example 1:






# Ruby program to illustrate the divide method 
  
# requires the set 
require "set"
  
s1 = Set[8, 5, 4]
  
  
# divide method used
puts s1.divide { |i,j| (i - j).abs == 3 }

Output:

Set: {#Set: {8, 5}, #Set: {4}}

Example 2:




# Ruby program to illustrate the divide method 
  
# requires the set 
require "set"
  
s1 = Set[16, 8, 3, 5, 2]
  
# divide method used
puts s1.divide { |i,j| (i - j)%2 == 0 }

Output:

Set: {#Set: {16, 8, 2}, #Set: {3, 5}}

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :