Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Set#to_set
() -> ::Set[A]
[T] () { (A) -> T } -> ::Set[T]
to_set {|element| ... } → new_set
to_set → self or new_set
set.c
View on GitHub ↗
static VALUE
set_i_to_set(VALUE set)
{
if (rb_obj_is_instance_of(set, rb_cSet) && !rb_block_given_p()) {
return set;
}
return rb_funcall_passing_block(rb_cSet, id_new, 1, &set);
}
With a block given, creates and returns a new set; calls the block with each element of self, and adds the block’s returns value to the new set:
Example
set = Set[*0..9] # => Set[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] set.to_set {|i| i * 2 } # => Set[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
With no block given, when self is an instance of Set, returns self:
Example
set = Set[*0..9] set.to_set set.to_set.equal?(set) # => true
With no block given, when self is an instance of a subclass of Set, returns a set containing the elements of self:
Example
class MySet < Set; end my_set = MySet[*0..9] # => #<MySet: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}> set = my_set.to_set # => Set[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Related: see Methods for Converting.
Ruby version changes
-
4.1
-
A deprecated behavior,
Set#to_set,Range#to_set, andEnumerable#to_setaccepting arguments, was removed. [Feature #21390]
-
-
4.0
-
Passing arguments to
Set#to_setandEnumerable#to_setis now deprecated. [Feature #21390]
-