[ruby-dev:29989] Module#constants excluding superclass
From:
Nobuyoshi Nakada <nobu@...>
Date:
2006-12-04 14:44:13 UTC
List:
ruby-dev #29989
なかだです。
http://jp.rubyist.net/magazine/?0017-CodeReview#l16 にあった件
のパッチです。
むしろ、そういう場合は const_get, const_defined? を使ってくれ、
といいたい気もしますが。
* intern.h, object.c, variable.c (rb_mod_constants): added an optional
flag to search ancestors, which is defaulted to true, as well as
const_defined? and const_get.
Index: eval.c
===================================================================
RCS file: /pub/cvs/ruby/eval.c,v
retrieving revision 1.958
diff -U 2 -p -r1.958 eval.c
--- eval.c 7 Nov 2006 09:38:12 -0000 1.958
+++ eval.c 4 Dec 2006 06:03:42 -0000
@@ -1907,9 +1907,13 @@ rb_mod_nesting(void)
static VALUE
-rb_mod_s_constants(void)
+rb_mod_s_constants(int argc, VALUE *argv, VALUE mod)
{
NODE *cbase = ruby_cref;
void *data = 0;
+ if (argc > 0) {
+ return rb_mod_constants(argc, argv, rb_cModule);
+ }
+
while (cbase) {
if (!NIL_P(cbase->nd_clss)) {
@@ -7953,5 +7957,5 @@ Init_eval(void)
rb_define_singleton_method(rb_cModule, "nesting", rb_mod_nesting, 0);
- rb_define_singleton_method(rb_cModule, "constants", rb_mod_s_constants, 0);
+ rb_define_singleton_method(rb_cModule, "constants", rb_mod_s_constants, -1);
rb_define_singleton_method(ruby_top_self, "include", top_include, -1);
Index: intern.h
===================================================================
RCS file: /pub/cvs/ruby/intern.h,v
retrieving revision 1.203
diff -U 2 -p -r1.203 intern.h
--- intern.h 30 Oct 2006 03:39:44 -0000 1.203
+++ intern.h 4 Dec 2006 03:40:31 -0000
@@ -563,5 +563,5 @@ void *rb_mod_const_at(VALUE, void*);
void *rb_mod_const_of(VALUE, void*);
VALUE rb_const_list(void*);
-VALUE rb_mod_constants(VALUE);
+VALUE rb_mod_constants(int, VALUE *, VALUE);
VALUE rb_mod_remove_const(VALUE, VALUE);
int rb_const_defined(VALUE, ID);
@@ -574,5 +574,4 @@ VALUE rb_const_get_from(VALUE, ID);
VALUE rb_const_get_fallback(VALUE, ID, struct RNode *);
void rb_const_set(VALUE, ID, VALUE);
-VALUE rb_mod_constants(VALUE);
VALUE rb_mod_const_missing(VALUE,VALUE);
VALUE rb_cvar_defined(VALUE, ID);
Index: object.c
===================================================================
RCS file: /pub/cvs/ruby/object.c,v
retrieving revision 1.208
diff -U 2 -p -r1.208 object.c
--- object.c 22 Nov 2006 08:34:18 -0000 1.208
+++ object.c 4 Dec 2006 02:59:56 -0000
@@ -2407,5 +2407,5 @@ Init_Object(void)
rb_class_local_methods, 0); /* in class.c */
- rb_define_method(rb_cModule, "constants", rb_mod_constants, 0); /* in variable.c */
+ rb_define_method(rb_cModule, "constants", rb_mod_constants, -1); /* in variable.c */
rb_define_method(rb_cModule, "const_get", rb_mod_const_get, -1);
rb_define_method(rb_cModule, "const_set", rb_mod_const_set, 2);
Index: variable.c
===================================================================
RCS file: /pub/cvs/ruby/variable.c,v
retrieving revision 1.141
diff -U 2 -p -r1.141 variable.c
--- variable.c 18 Sep 2006 01:59:00 -0000 1.141
+++ variable.c 4 Dec 2006 06:17:21 -0000
@@ -1439,15 +1439,36 @@ rb_const_list(void *data)
/*
* call-seq:
- * mod.constants => array
- *
+ * mod.constants(inherit=true) => array
+ *
* Returns an array of the names of the constants accessible in
* <i>mod</i>. This includes the names of constants in any included
- * modules (example at start of section).
+ * modules (example at start of section), unless the <i>all</i>
+ * parameter is set to <code>false</code>.
+ *
+ * IO.constants.include?("SYNC") => true
+ * IO.constants(false).include?("SYNC") => false
+ *
+ * Also see <code>Module::const_defined?</code>.
*/
VALUE
-rb_mod_constants(VALUE mod)
+rb_mod_constants(int argc, VALUE *argv, VALUE mod)
{
- return rb_const_list(rb_mod_const_of(mod, 0));
+ VALUE inherit;
+ st_table *tbl;
+
+ if (argc == 0) {
+ inherit = Qtrue;
+ }
+ else {
+ rb_scan_args(argc, argv, "01", &inherit);
+ }
+ if (RTEST(inherit)) {
+ tbl = rb_mod_const_of(mod, 0);
+ }
+ else {
+ tbl = rb_mod_const_at(mod, 0);
+ }
+ return rb_const_list(tbl);
}
--
--- 僕の前にBugはない。
--- 僕の後ろにBugはできる。
中田 伸悦