mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* eval.c (rb_overlay_module, rb_mod_refine): accept a module as the
argument of Module#refine. * vm_method.c (search_method): if klass is an iclass, lookup the original module of the iclass in omod in order to allow refinements of modules. * test/ruby/test_refinement.rb: add tests for the above changes. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37040 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
e0bff65092
commit
8940e37210
4 changed files with 94 additions and 7 deletions
11
ChangeLog
11
ChangeLog
|
@ -1,3 +1,14 @@
|
|||
Thu Sep 27 18:36:51 2012 Shugo Maeda <shugo@ruby-lang.org>
|
||||
|
||||
* eval.c (rb_overlay_module, rb_mod_refine): accept a module as the
|
||||
argument of Module#refine.
|
||||
|
||||
* vm_method.c (search_method): if klass is an iclass, lookup the
|
||||
original module of the iclass in omod in order to allow
|
||||
refinements of modules.
|
||||
|
||||
* test/ruby/test_refinement.rb: add tests for the above changes.
|
||||
|
||||
Thu Sep 27 18:12:20 2012 Aaron Patterson <aaron@tenderlovemaking.com>
|
||||
|
||||
* ext/syslog/lib/syslog/logger.rb: add a formatter to the
|
||||
|
|
14
eval.c
14
eval.c
|
@ -1030,12 +1030,22 @@ rb_mod_prepend(int argc, VALUE *argv, VALUE module)
|
|||
return module;
|
||||
}
|
||||
|
||||
static
|
||||
void check_class_or_module(VALUE obj)
|
||||
{
|
||||
if (!RB_TYPE_P(obj, T_CLASS) && !RB_TYPE_P(obj, T_MODULE)) {
|
||||
VALUE str = rb_inspect(obj);
|
||||
rb_raise(rb_eTypeError, "%s is not a class/module",
|
||||
StringValuePtr(str));
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
rb_overlay_module(NODE *cref, VALUE klass, VALUE module)
|
||||
{
|
||||
VALUE iclass, c, superclass = klass;
|
||||
|
||||
Check_Type(klass, T_CLASS);
|
||||
check_class_or_module(klass);
|
||||
Check_Type(module, T_MODULE);
|
||||
if (NIL_P(cref->nd_omod)) {
|
||||
cref->nd_omod = rb_hash_new();
|
||||
|
@ -1184,7 +1194,7 @@ rb_mod_refine(VALUE module, VALUE klass)
|
|||
ID id_overlaid_modules, id_refined_class;
|
||||
VALUE overlaid_modules;
|
||||
|
||||
Check_Type(klass, T_CLASS);
|
||||
check_class_or_module(klass);
|
||||
CONST_ID(id_overlaid_modules, "__overlaid_modules__");
|
||||
overlaid_modules = rb_attr_get(module, id_overlaid_modules);
|
||||
if (NIL_P(overlaid_modules)) {
|
||||
|
|
|
@ -324,5 +324,61 @@ class TestRefinement < Test::Unit::TestCase
|
|||
obj = c.new
|
||||
assert_equal([:c, :m1, :m2], m2.module_eval { obj.foo })
|
||||
end
|
||||
end
|
||||
|
||||
def test_refine_module_without_overriding
|
||||
m1 = Module.new
|
||||
c = Class.new {
|
||||
include m1
|
||||
}
|
||||
m2 = Module.new {
|
||||
refine m1 do
|
||||
def foo
|
||||
:m2
|
||||
end
|
||||
end
|
||||
}
|
||||
obj = c.new
|
||||
assert_equal(:m2, m2.module_eval { obj.foo })
|
||||
end
|
||||
|
||||
def test_refine_module_with_overriding
|
||||
m1 = Module.new {
|
||||
def foo
|
||||
[:m1]
|
||||
end
|
||||
}
|
||||
c = Class.new {
|
||||
include m1
|
||||
}
|
||||
m2 = Module.new {
|
||||
refine m1 do
|
||||
def foo
|
||||
super << :m2
|
||||
end
|
||||
end
|
||||
}
|
||||
obj = c.new
|
||||
assert_equal([:m1, :m2], m2.module_eval { obj.foo })
|
||||
end
|
||||
|
||||
def test_refine_neither_class_nor_module
|
||||
assert_raise(TypeError) do
|
||||
Module.new {
|
||||
refine Object.new do
|
||||
end
|
||||
}
|
||||
end
|
||||
assert_raise(TypeError) do
|
||||
Module.new {
|
||||
refine 123 do
|
||||
end
|
||||
}
|
||||
end
|
||||
assert_raise(TypeError) do
|
||||
Module.new {
|
||||
refine "foo" do
|
||||
end
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
14
vm_method.c
14
vm_method.c
|
@ -385,11 +385,21 @@ search_method(VALUE klass, ID id, VALUE omod, VALUE *defined_class_ptr)
|
|||
for (body = 0; klass; klass = RCLASS_SUPER(klass)) {
|
||||
st_table *m_tbl;
|
||||
|
||||
if (!NIL_P(omod) && klass != skipped_class &&
|
||||
!NIL_P(iclass = rb_hash_lookup(omod, klass))) {
|
||||
if (!NIL_P(omod) && klass != skipped_class) {
|
||||
VALUE c;
|
||||
|
||||
if (BUILTIN_TYPE(klass) == T_ICLASS) {
|
||||
c = RBASIC(klass)->klass;
|
||||
}
|
||||
else {
|
||||
c = klass;
|
||||
}
|
||||
iclass = rb_hash_lookup(omod, c);
|
||||
if (!NIL_P(iclass)) {
|
||||
skipped_class = klass;
|
||||
klass = iclass;
|
||||
}
|
||||
}
|
||||
m_tbl = RCLASS_M_TBL(klass);
|
||||
if (!m_tbl) {
|
||||
m_tbl = RCLASS_M_TBL(RCLASS_ORIGIN(RBASIC(klass)->klass));
|
||||
|
|
Loading…
Reference in a new issue