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:
require "set"
s1 = Set[ 8 , 5 , 4 ]
puts s1.divide { |i,j| (i - j).abs == 3 }
|
Output:
Set: {#Set: {8, 5}, #Set: {4}}
Example 2:
require "set"
s1 = Set[ 16 , 8 , 3 , 5 , 2 ]
puts s1.divide { |i,j| (i - j)% 2 == 0 }
|
Output:
Set: {#Set: {16, 8, 2}, #Set: {3, 5}}
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...