instance method
Range#to_set
() -> ::Set[Elem]
[T] () { (Elem) -> T } -> ::Set[T]
to_set → set
range.c
View on GitHub ↗
static VALUE
range_to_set(int argc, VALUE *argv, VALUE range)
{
if (NIL_P(RANGE_END(range))) {
rb_raise(rb_eRangeError, "cannot convert endless range to a set");
}
return rb_call_super(argc, argv);
}
Returns a set containing the elements in self, if a finite collection; raises an exception otherwise.
Example
(1..4).to_set # => Set[1, 2, 3, 4] (1...4).to_set # => Set[1, 2, 3] (1..).to_set # in 'Range#to_set': cannot convert endless range to a set (RangeError)
Ruby version changes
-
4.0
-
Range#to_setnow performs size checks to prevent issues with endless ranges. [Bug #21654]
-