class method
Set.new
[unchecked out A] (::_Each[A]) -> ::Set[A]
[unchecked out A, X] (::_Each[X]) { (X) -> A } -> ::Set[A]
[unchecked out A] (?nil) -> ::Set[A]
Set.new → new_set
Set.new(enum) → new_set
Set.new(enum) { |elem| ... } → new_set
set.c
View on GitHub ↗
static VALUE
set_i_initialize(int argc, VALUE *argv, VALUE set)
{
if (RBASIC(set)->flags & RSET_INITIALIZED) {
rb_raise(rb_eRuntimeError, "cannot reinitialize set");
}
RBASIC(set)->flags |= RSET_INITIALIZED;
VALUE other;
rb_check_arity(argc, 0, 1);
if (argc > 0 && (other = argv[0]) != Qnil) {
if (RB_TYPE_P(other, T_ARRAY)) {
long i;
int block_given = rb_block_given_p();
set_table *into = RSET_TABLE(set);
for (i=0; i<RARRAY_LEN(other); i++) {
VALUE key = RARRAY_AREF(other, i);
if (block_given) key = rb_yield(key);
set_table_insert_wb(into, set, key, NULL);
}
}
else {
rb_block_call(other, enum_method_id(other), 0, 0,
rb_block_given_p() ? set_initialize_with_block : set_initialize_without_block,
set);
}
}
return set;
}
Creates a new set containing the elements of the given enumerable object.
If a block is given, the elements of enum are preprocessed by the given block.
Example
Set.new([1, 2]) #=> Set[1, 2] Set.new([1, 2, 1]) #=> Set[1, 2] Set.new([1, 'c', :s]) #=> Set[1, "c", :s] Set.new(1..5) #=> Set[1, 2, 3, 4, 5] Set.new([1, 2, 3]) { |x| x * x } #=> Set[1, 4, 9]