[前][次][番号順一覧][スレッド一覧]

ruby-changes:70499

From: zverok <ko1@a...>
Date: Fri, 24 Dec 2021 10:31:14 +0900 (JST)
Subject: [ruby-changes:70499] 34deea3b42 (master): Add docs for Refinement class

https://git.ruby-lang.org/ruby.git/commit/?id=34deea3b42

From 34deea3b42189cd46e88f4a459bd4fc58940aa60 Mon Sep 17 00:00:00 2001
From: zverok <zverok.offline@g...>
Date: Wed, 15 Dec 2021 00:08:36 +0200
Subject: Add docs for Refinement class

---
 class.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 eval.c  |  7 +------
 2 files changed, 58 insertions(+), 6 deletions(-)

diff --git a/class.c b/class.c
index aefeffb22d9..f83a16a08e8 100644
--- a/class.c
+++ b/class.c
@@ -700,6 +700,58 @@ boot_defclass(const char *name, VALUE super) https://github.com/ruby/ruby/blob/trunk/class.c#L700
     return obj;
 }
 
+/***********************************************************************
+ *
+ * Document-class: Refinement
+ *
+ *  Refinement is a class of the +self+ (current context) inside +refine+
+ *  statement. It allows to import methods from other modules, see #import_methods.
+ */
+
+#if 0 /* for RDoc */
+/*
+ * Document-method: Refinement#import_methods
+ *
+ *  call-seq:
+ *     import_methods(module, ...)    -> self
+ *
+ *  Imports methods from modules. Unlike Module#include,
+ *  Refinement#import_methods copies methods and adds them into the refinement,
+ *  so the refinement is activated in the imported methods.
+ *
+ *  Note that due to method copying, only methods defined in Ruby code can be imported.
+ *
+ *     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
+ *
+ */
+
+static VALUE
+refinement_import_methods(int argc, VALUE *argv, VALUE refinement)
+{
+}
+# endif
+
 void
 Init_class_hierarchy(void)
 {
@@ -714,6 +766,11 @@ Init_class_hierarchy(void) https://github.com/ruby/ruby/blob/trunk/class.c#L766
     rb_cClass =  boot_defclass("Class",  rb_cModule);
     rb_cRefinement =  boot_defclass("Refinement",  rb_cModule);
 
+#if 0 /* for RDoc */
+    // we pretend it to be public, otherwise RDoc will ignore it
+    rb_define_method(rb_cRefinement, "import_methods", refinement_import_methods, -1);
+#endif
+
     rb_const_set(rb_cObject, rb_intern_const("BasicObject"), rb_cBasicObject);
     RBASIC_SET_CLASS(rb_cClass, rb_cClass);
     RBASIC_SET_CLASS(rb_cModule, rb_cClass);
diff --git a/eval.c b/eval.c
index 14241397105..0de3105ac79 100644
--- a/eval.c
+++ b/eval.c
@@ -1535,12 +1535,7 @@ refinement_import_methods_i(ID key, VALUE value, void *data) https://github.com/ruby/ruby/blob/trunk/eval.c#L1535
 }
 
 /*
- *  call-seq:
- *     import_methods(module, ...)    -> self
- *
- *  Imports methods from modules. Unlike Module#include,
- *  Refinement#import_methods copies methods and adds them into the refinement,
- *  so the refinement is activated in the imported methods.
+ * Note: docs for the method are in class.c
  */
 
 static VALUE
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]