Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Set#divide

() { (A, A) -> ::Hash::_Key } -> ::Set[self]
() { (A) -> ::Hash::_Key } -> ::Set[self]
divide(&func)

Divides the set into a set of subsets according to the commonality defined by the given block.

If the arity of the block is 2, elements o1 and o2 are in common if block.call(o1, o2) is true. Otherwise, elements o1 and o2 are in common if block.call(o1) == block.call(o2).

Example
require 'set'
numbers = Set[1, 3, 4, 6, 9, 10, 11]
set = numbers.divide { |i,j| (i - j).abs == 1 }
set        #=> #<Set: {#<Set: {1}>,
           #           #<Set: {11, 9, 10}>,
           #           #<Set: {3, 4}>,
           #           #<Set: {6}>}>

Returns an enumerator if no block is given.

See also #disjoint? #each #difference #empty? also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0