Skip to main content

Ruby Set divide method

divide() Set method in ruby programming language are used to generate subset according to commonality defined by the given block.

Example of divide() set method

# Example 1
require "set"

s = Set[2,9,6,4,8,5]

# Collect all subsets which adjacent elements differ by one 
result = s.divide { |x,y| (x-y) == -1 || (x-y) == 1}

# Display result value
print("result : ",result.to_a)  
Ruby set divide method example 1
result : [#<Set: {2}>, #<Set: {9, 8}>, #<Set: {6, 5, 4}>]


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment