Method: Object#swap

Defined in:
ext/evilr/evilr.c

#swap(other) ⇒ self

Swap the contents of the receiver with other:

a = []
b = {}
a.swap(b) # => {}
a # => {}
b # => []

You cannot swap a Class or Module except with another Class or Module, and you can only swap a Class with a Class and a Module with a Module (no swapping a Class with Module), and you cannot swap immediate values. If an invalid swap attempt is detected, a TypeError is raised.

Returns:

  • (self)


300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'ext/evilr/evilr.c', line 300

static VALUE evilr_swap(VALUE self, VALUE other) {
  char tmp[OBJECT_SIZE];
  evilr__check_immediates(self, other);
  if ((BUILTIN_TYPE(self) == T_MODULE || BUILTIN_TYPE(self) == T_CLASS ||
       BUILTIN_TYPE(other) == T_MODULE || BUILTIN_TYPE(other) == T_CLASS) &&
       BUILTIN_TYPE(self) != BUILTIN_TYPE(other)) {
    rb_raise(rb_eTypeError, "incompatible types used");
  }
  memcpy(tmp, ROBJECT(self), OBJECT_SIZE);
  memcpy(ROBJECT(self), ROBJECT(other), OBJECT_SIZE);
  memcpy(ROBJECT(other), tmp, OBJECT_SIZE);
  return self;
}