Method: List#initialize_copy

Defined in:
ext/list/list.c

#initialize_copy(orig) ⇒ Object



526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'ext/list/list.c', line 526

static VALUE
list_replace(VALUE copy, VALUE orig)
{
	item_t *c_orig;
	item_t *c_copy;
	long olen;

	list_modify_check(copy);
	if (copy == orig) return copy;

	switch (rb_type(orig)) {
	case T_ARRAY:
		return list_replace_ary(copy, orig);
	case T_DATA:
		break;
	default:
		rb_raise(rb_eTypeError, "cannot convert to list");
	}
	orig = to_list(orig);
	olen = LIST_LEN(orig);
	if (olen == 0) {
		return list_clear(copy);
	}
	if (olen == LIST_LEN(copy)) {
		LIST_FOR_DOUBLE(orig, c_orig, copy, c_copy, {
			c_copy->value = c_orig->value;
		});
	} else {
		list_clear(copy);
		LIST_FOR(orig, c_orig) {
			list_push(copy, c_orig->value);
		}
	}

	return copy;
}