Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Set#flatten

() -> ::Set[untyped]
flatten → new_set

Returns a new set that is a copy of self, but with self and its nested sets flattened; that is, their elements become elements of self:

Example
Set[Set[0, 1], Set[2, 3]].flatten
# => Set[0, 1, 2, 3]
Set[Set[0, 1], Set[Set[2, 3], Set[3, 4]]].flatten
# => Set[0, 1, 2, 3, 4]

Does not flatten nested arrays or hashes:

Example
Set[%w[foo bar]].flatten      # => Set[["foo", "bar"]]
Set[{foo: 0, bar: 1}].flatten # => Set[{foo: 0, bar: 1}]

Related: see Methods for Converting.

See also #flatten! #filter! #eql? #hash also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0