summaryrefslogtreecommitdiff
path: root/eval.c
diff options
context:
space:
mode:
authorshugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-06-12 14:33:59 +0000
committershugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-06-12 14:33:59 +0000
commit17f1cdaa00ff51470b934c99f6e54a24bf377b71 (patch)
tree7947480ed244b4ca4f1de8e352e759cf2fe317e7 /eval.c
parent1f828497d1e8df2b7b68ac2a093ab4439585d88a (diff)
* eval.c (mod_using): new method Module#using, which activates
refinements of the specified module only in the current class or module definition. [ruby-core:55273] [Feature #8481] * test/ruby/test_refinement.rb: related test. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'eval.c')
-rw-r--r--eval.c32
1 files changed, 31 insertions, 1 deletions
diff --git a/eval.c b/eval.c
index c0d64e8717..f38f1c250e 100644
--- a/eval.c
+++ b/eval.c
@@ -1226,6 +1226,34 @@ rb_mod_refine(VALUE module, VALUE klass)
return refinement;
}
+/*
+ * call-seq:
+ * using(module) -> self
+ *
+ * Import class refinements from <i>module</i> into the current class or
+ * module definition.
+ */
+
+static VALUE
+mod_using(VALUE self, VALUE module)
+{
+ NODE *cref = rb_vm_cref();
+ rb_control_frame_t *prev_cfp = previous_frame(GET_THREAD());
+
+ warn_refinements_once();
+ if (prev_frame_func()) {
+ rb_raise(rb_eRuntimeError,
+ "Module#using is not permitted in methods");
+ }
+ if (prev_cfp && prev_cfp->self != self) {
+ rb_raise(rb_eRuntimeError, "Module#using is not called on self");
+ }
+ Check_Type(module, T_MODULE);
+ rb_using_module(cref, module);
+ rb_clear_cache();
+ return self;
+}
+
void
rb_obj_call_init(VALUE obj, int argc, VALUE *argv)
{
@@ -1354,7 +1382,8 @@ top_using(VALUE self, VALUE module)
warn_refinements_once();
if (cref->nd_next || (prev_cfp && prev_cfp->me)) {
- rb_raise(rb_eRuntimeError, "using is permitted only at toplevel");
+ rb_raise(rb_eRuntimeError,
+ "main.using is permitted only at toplevel");
}
Check_Type(module, T_MODULE);
rb_using_module(cref, module);
@@ -1558,6 +1587,7 @@ Init_eval(void)
rb_define_private_method(rb_cModule, "prepend_features", rb_mod_prepend_features, 1);
rb_define_private_method(rb_cModule, "prepend", rb_mod_prepend, -1);
rb_define_private_method(rb_cModule, "refine", rb_mod_refine, 1);
+ rb_define_private_method(rb_cModule, "using", mod_using, 1);
rb_undef_method(rb_cClass, "refine");
rb_undef_method(rb_cClass, "module_function");