Method: Object#define!

Defined in:
ext/rmtools.cpp

#define!(new_obj) ⇒ Object

Assign new value by variable or constant address. Can’t process numeric, nil, false and true. Though, it can process containers with all of that shit.

a = 'Object.new'
=> "Object.new"
def inspire x
    x.define! eval x
end
=> nil
inspire a
=> #<Object:0xb790bed0>
a
=> #<Object:0xb790bed0>

It’s quite buggy, you can get sudden segfault sometime after using method ^_^‘\ Maybe it could mark used objects for GC not to collect



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'ext/rmtools.cpp', line 20

static VALUE object_define_new_value(VALUE self, VALUE new_obj)
{
    if (FIXNUM_P(self) || self == Qnil || self == Qfalse || self == Qtrue || self == Qundef) {
        VALUE tmp = rb_mod_name(rb_obj_class(self));
        const char* msg = StringValuePtr(tmp);
        rb_raise(rb_eTypeError, "can't redefine %s", msg);
    }
    if (FIXNUM_P(new_obj) || new_obj == Qnil || new_obj == Qfalse || new_obj == Qtrue  || new_obj == Qundef) {
        VALUE tmp = rb_mod_name(rb_obj_class(self));
        const char* msg = StringValuePtr(tmp);
        rb_raise(rb_eTypeError, "can't define object as %s", msg);
    }
    // Place the definition of the new object in the slot of self
    memcpy(reinterpret_cast<void*>(self), reinterpret_cast<void*>(rb_funcall(new_obj, rb_intern("clone"), 0)), SLOT_SIZE);
    return self;
}