Module#constants excluding superclass

e$B$J$+$@$G$9!#e(B

http://jp.rubyist.net/magazine/?0017-CodeReview#l16 e$B$K$"$C$?7oe(B
e$B$N%Q%C%A$G$9!#e(B

e$B$`$7$m!"$=$&$$$&>l9g$Oe(B const_get, const_defined?
e$B$r;H$C$F$/$l!"e(B
e$B$H$$$$$?$$5$$b$7$^$9$,!#e(B

  • 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
    • mod. This includes the names of constants in any included
    • modules (example at start of section).
    • modules (example at start of section), unless the all
    • parameter is set to false.
    • IO.constants.include?(“SYNC”) => true
    • IO.constants(false).include?(“SYNC”) => false
    • Also see Module::const_defined?.
      */

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);
    }

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:29989] Module#constants excluding superclass”
on Mon, 4 Dec 2006 23:44:13 +0900, Nobuyoshi N.
[email protected] writes:

|http://jp.rubyist.net/magazine/?0017-CodeReview#l16 e$B$K$"$C$?7oe(B
|e$B$N%Q%C%A$G$9!#e(B

e$B%3%_%C%H$7$F$/$@$5$$e(B(1.9e$B$Ke(B)e$B!#e(B

e$B@DLZ$G$9!#e(B

In mail “[ruby-dev:29989] Module#constants excluding superclass”
Nobuyoshi N. [email protected] wrote:

e$B$J$+$@$G$9!#e(B

http://jp.rubyist.net/magazine/?0017-CodeReview#l16 e$B$K$"$C$?7oe(B
e$B$N%Q%C%A$G$9!#e(B

e$B$`$7$m!"$=$&$$$&>l9g$Oe(B const_get, const_defined? e$B$r;H$C$F$/$l!"e(B
e$B$H$$$$$?$$5$$b$7$^$9$,!#e(B

e$B$$$(!“$”$l$O!"e(Bconst_get e$B$de(B const_defined?
e$B$G$O%@%a$@$+$i:$$k$H$$$&e(B
e$B$3$H$G$9!#8=>u$N;EMM$G$O!"e(B

class A
CONST = 1
end
class B
CONST = 1
end

e$B$3$N$h$&$J%/%i%9e(B A, B e$B$,$"$k$H$-!"e(BCONST e$B$,e(B B
e$B$ND>2<$Ge(B (e$B:Fe(B) e$BDj5A$5$le(B
e$B$?$+$I$&$+H=Dj$G$-$^$;$s!#e(B

~ % cat t
class A
CONST = 1
end
class B < A
CONST = 1
end

p A.const_defined?(:CONST) == B.const_defined?(:CONST)
p A.const_get(:CONST) == B.const_get(:CONST)
p A.const_get(:CONST).equal?(B.const_get(:CONST))
p A.constants.sort == B.constants.sort

~ % forall-ruby t
ruby 1.8.0 (2003-08-04) [x86_64-linux]
true
true
true
true
ruby 1.8.1 (2003-12-25) [x86_64-linux]
true
true
true
true
ruby 1.8.2 (2004-12-25) [x86_64-linux]
true
true
true
true
ruby 1.8.3 (2005-09-21) [x86_64-linux]
true
true
true
true
ruby 1.8.4 (2005-12-24) [x86_64-linux]
true
true
true
true
ruby 1.8.5 (2006-08-25) [x86_64-linux]
true
true
true
true
ruby 1.8.5 (2006-10-23) [x86_64-linux]
true
true
true
true
ruby 1.9.0 (2006-12-04 patchlevel 0) [x86_64-linux]
true
true
true
true

e$B@DLZ$G$9!#e(B

In mail “[ruby-dev:29992] Re: Module#constants excluding superclass”
Nobuyoshi N. [email protected] wrote:

e$B$J$+$@$G$9!#e(B

1.9e$B$J$i!"e(BB.const_defined?(:CONST, false)e$B$GH=JL$G$-$k$O$:$G$9!#e(B

e$B$J$L!<!#$=$C$A$O0z?t$D$$$F$?$s$G$9$+!#<:Ni$7$^$7$?!#e(B

e$B%j%U%!%l%s%9$P$C$+8+$F$?$N$,GT0x$@$C$?!#e(B

e$B?7$7$$$[$&$ODI2C$7$H$3$&e(B

e$B$J$+$@$G$9!#e(B

At Tue, 5 Dec 2006 00:16:52 +0900,
Minero A. wrote in [ruby-dev:29991]:

class A
CONST = 1
end
class B
CONST = 1
end

e$B$3$N$h$&$J%/%i%9e(B A, B e$B$,$"$k$H$-!"e(BCONST e$B$,e(B B e$B$ND>2<$Ge(B (e$B:Fe(B) e$BDj5A$5$le(B
e$B$?$+$I$&$+H=Dj$G$-$^$;$s!#e(B

1.9e$B$J$i!"e(BB.const_defined?(:CONST,
false)e$B$GH=JL$G$-$k$O$:$G$9!#e(B