Ruby 2.0 ????????????
???????????? ???????[ 2014/3/14 13:52:26 ] ????????Ruby ?????? ????
?????????????????????? Module#include ??????????????????????????????????????????????????? prepend_features ?????? prepended ???????????Module#prepend_features ??????????????????????????????
static VALUE
rb_mod_prepend_features(VALUE module?? VALUE prepend)
{
switch (TYPE(prepend)) {
case T_CLASS:
case T_MODULE:
break;
default:
Check_Type(prepend?? T_CLASS);
break;
}
rb_prepend_module(prepend?? module);
return module;
}
???????????Щ?????????飬????????????? rb_prepend_module ????????????? rb_prepend_module ????????????
void
rb_prepend_module(VALUE klass?? VALUE module)
{
void rb_vm_check_redefinition_by_prepend(VALUE klass);
VALUE origin;
int changed = 0;
rb_frozen_class_p(klass);
if (!OBJ_UNTRUSTED(klass)) {
rb_secure(4);
}
Check_Type(module?? T_MODULE);
OBJ_INFECT(klass?? module);
origin = RCLASS_ORIGIN(klass);
if (origin == klass) {
origin = class_alloc(T_ICLASS?? klass);
RCLASS_SUPER(origin) = RCLASS_SUPER(klass);
RCLASS_SUPER(klass) = origin;
RCLASS_ORIGIN(klass) = origin;
RCLASS_M_TBL(origin) = RCLASS_M_TBL(klass);
RCLASS_M_TBL(klass) = st_init_numtable();
st_foreach(RCLASS_M_TBL(origin)?? move_refined_method??
(st_data_t) RCLASS_M_TBL(klass));
}
changed = include_modules_at(klass?? klass?? module);
if (changed < 0)
rb_raise(rb_eArgError?? "cyclic prepend detected");
if (changed) {
rb_clear_cache();
rb_vm_check_redefinition_by_prepend(klass);
}
}
????????????????Щ????????????????????? 16 ?ж????????Щ????????????????????????? 17 ?п????????
??????????? RCLASS_ORIGIN ??? klass ?? origin ?????????????? klass ???????????? origin ??????????????????????????????棬?? klass ?? origin ???????????????????????? if ????е??????
????19 ??? klass ??????????μ????????????????????
????20 ~ 21 ?а??′????????????? klass ?? klass ??????м?
????22 ?н? klass ?? origin ????????????
????????????23 ~ 24 ?а? klass ????????????????У?????? klass ????????
??????25 ????? klass ??????????е? Refined ?????????????
?????????? if ????????????????????? 28 ?С???????????????????????????????? include_modules_at ????????????????У???????????????????????????????????顣???????????????????????????????????飬??????????????????
???????????????????????顣???????????????????????? klass ?? klass ????????????? klass ????????????????????ε??????У?????????λ?????ú????Ruby ???????任????????????????????????飬??????
??????????????????????????????????У??? C ?? prepend A?? B ??????????????
????+-----+ +--------+
????Before: | C |----->| Object |
????+-----+ +--------+
????+--------------- klass ----------------+
????| |
????v |
????+-----+ +-----+ +-----+ +-----+ +--------+
????After: | C |----->| A |----->| B |----->| C' |+---->| Object |
????+-----+ +-----+ +-----+ +-----+ +--------+
????| ^
????| |
????+--------------- origin ---------------+
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11