class Refinement
Refinement 是 refine 语句中 self(当前上下文)的一个类。它允许从其他模块导入方法,请参见 import_methods。
Public Instance Methods
Source
static VALUE
refinement_import_methods(int argc, VALUE *argv, VALUE refinement)
{
}
从模块导入方法。与 Module#include 不同,Refinement#import_methods 会复制方法并将其添加到 refinement 中,因此 refinement 在导入的方法中被激活。
请注意,由于方法复制,只能导入在 Ruby 代码中定义的方法。
module StrUtils def indent(level) ' ' * level + self end end module M refine String do import_methods StrUtils end end using M "foo".indent(3) #=> " foo" module M refine String do import_methods Enumerable # Can't import method which is not defined with Ruby code: Enumerable#drop end end
也别名化为:import_methods
Source
VALUE
rb_refinement_module_get_refined_class(VALUE module)
{
ID id_refined_class;
CONST_ID(id_refined_class, "__refined_class__");
return rb_attr_get(module, id_refined_class);
}
返回被接收者 refinement 的类或模块。
module M refine String do end end M.refinements[0].target # => String