Class: Class
Instance Method Summary collapse
-
#detach_singleton ⇒ self
If the receiver is a singleton class, it is transformed into a regular class and it is detached from the instance.
-
#inherit(*classes) ⇒ self
Make copies of all given
classes
as modules, and includes those modules in the receiver. -
#singleton_class_instance ⇒ Object || nil
Returns the object attached to the singleton class.
-
#superclass=(klass) ⇒ Object
Modifies the superclass of the current class to be the given class.
-
#to_module ⇒ Module
Makes a copy of the class, converts the copy to a module, and returns it.
Instance Method Details
#detach_singleton ⇒ self
If the receiver is a singleton class, it is transformed into a regular class and it is detached from the instance. Note that this means it becomes the class of the object to which it was previous attached. If the receiver is not a singleton class, has no effect. Returns the receiver.
589 590 591 592 593 594 595 596 597 |
# File 'ext/evilr/evilr.c', line 589
static VALUE evilr_detach_singleton(VALUE klass) {
if (IS_SINGLETON_CLASS(klass)) {
FL_UNSET(klass, FL_SINGLETON);
if (RCLASS_IV_TBL(klass)) {
st_delete(RCLASS_IV_TBL(klass), (st_data_t*)&evilr__attached, 0);
}
}
return klass;
}
|
#inherit(*classes) ⇒ self
Make copies of all given classes
as modules, and includes those modules in the receiver. Raises TypeError
if any of the classes
is not a Class or is not compatible with the receiver.
812 813 814 815 816 817 818 819 820 821 |
# File 'ext/evilr/evilr.c', line 812
static VALUE evilr_inherit(int argc, VALUE* argv, VALUE klass) {
int i;
for(i = 0; i < argc; i++) {
evilr__check_compatible_classes(klass, argv[i]);
rb_include_module(klass, evilr_to_module(argv[i]));
}
return klass;
}
|
#singleton_class_instance ⇒ Object || nil
Returns the object attached to the singleton class. If the class does not have an object attached to it (possibly because it isn’t a singleton class), returns nil
.
724 725 726 727 728 729 730 |
# File 'ext/evilr/evilr.c', line 724
static VALUE evilr_singleton_class_instance(VALUE klass) {
VALUE obj;
if(RCLASS_IV_TBL(klass) && st_lookup(RCLASS_IV_TBL(klass), evilr__attached, &obj)) {
return obj;
}
return Qnil;
}
|
#superclass=(klass) ⇒ Object
Modifies the superclass of the current class to be the given class. Any modules included in the receiver remain included. Raises TypeError
if klass is not a Class or if the receiver and class are not compatible (where their instances use different internal types).
797 798 799 800 801 802 803 804 |
# File 'ext/evilr/evilr.c', line 797
static VALUE evilr_superclass_e(VALUE klass, VALUE super) {
VALUE iclass;
evilr__check_compatible_classes(klass, super);
iclass = evilr__iclass_before_next_class(klass);
RCLASS_SET_SUPER(iclass, super);
rb_clear_cache_by_class(klass);
return super;
}
|
#to_module ⇒ Module
Makes a copy of the class, converts the copy to a module, and returns it. The returned module can be included in other classes.
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 |
# File 'ext/evilr/evilr.c', line 737
static VALUE evilr_to_module(VALUE klass) {
VALUE mod, iclass;
if (IS_SINGLETON_CLASS(klass)) {
if((mod = evilr_singleton_class_instance(klass))) {
mod = rb_singleton_class_clone(mod);
(void)evilr_detach_singleton(mod);
} else {
rb_raise(rb_eTypeError, "singleton class without attached instance");
}
} else {
mod = rb_obj_clone(klass);
}
RBASIC_SET_KLASS(mod, rb_cModule);
iclass = RCLASS_SUPER(mod);
RCLASS_SET_SUPER(mod, NULL);
FL_UNSET(mod, T_MASK);
FL_SET(mod, T_MODULE);
evilr__include_iclasses(mod, iclass);
return mod;
}
|