Open In App

Ruby | Set divide() function

Improve
Improve
Like Article
Like
Save
Share
Report

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}}

Last Updated : 07 Jan, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads