Ruby | Set divide() function

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Set: {#Set: {16, 8, 2}, #Set: {3, 5}}
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.