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}}
Recommended Posts:
- Ruby | Set replace() function
- Ruby | Set reset() function
- Ruby | Set merge() function
- Ruby | Set member?() function
- Ruby | Set include?() function
- Ruby | Set delete? function
- Ruby | Set delete() function
- Ruby | Set compare_by_identity? function
- Ruby | Set compare_by_identity() function
- Ruby | Set add? function
- Ruby | Set clear() function
- Ruby | Set difference() function
- Ruby | Set disjoint? function
- Ruby | Set flatten() function
- Ruby | Set flatten!() function
- Ruby | Set intersect?() function
- Ruby | Set intersection() function
- Ruby | Set length() function
- Ruby | Set size() function
- Ruby | Set proper_subset?() function
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.

