From: Ondrej Bilka Date: 2006-09-12T19:55:54+09:00 Subject: constants (rcrs 178, 343,345) I looked at my rcr 343 and 345. Could you give me some feedback? First question is what ways of constant resolution would you use at rcr. Add to source get_const where is constant expected? Change parser that it search constants from called method's class too? Write AI that recognizes to what class constant belongs? Have somebody other idea? How abreviate constants(FIXNUM_PROCESS_RLIMIT_CPU -> CPU)? I use conversion that you strip prefix and add as new constant while it makes sense. It is because you could specify what abbrevs do you want and what not. It producess enought spam warnings to encourage resolving abbrevation clashes. Resolution abrevs could be done when get_const is called but I don't see any advantage of this and don't know how convert ID to char *. This is my try. It shows way how modify other code(It will take long so I would do it if it will me merged.) Index: process.c =================================================================== RCS file: /src/ruby/process.c,v retrieving revision 1.156 diff -u -r1.156 process.c --- process.c 4 Sep 2006 07:40:43 -0000 1.156 +++ process.c 12 Sep 2006 09:51:27 -0000 @@ -715,7 +715,7 @@ else { pid = NUM2INT(vpid); if (argc == 2 && !NIL_P(vflags)) { - flags = NUM2UINT(vflags); + flags = NUM2UINT(rb_constantize(rb_mProcess,vflags)); } } if ((pid = rb_waitpid(pid, &status, flags)) < 0) @@ -1852,7 +1852,7 @@ int prio, iwhich, iwho; rb_secure(2); - iwhich = NUM2INT(which); + iwhich = NUM2INT(rb_get_const(rb_mProcess,which)); iwho = NUM2INT(who); errno = 0; @@ -1884,7 +1884,7 @@ int iwhich, iwho, iprio; rb_secure(2); - iwhich = NUM2INT(which); + iwhich = NUM2INT(rb_get_const(rb_mProcess,which)); iwho = NUM2INT(who); iprio = NUM2INT(prio); @@ -1934,7 +1934,7 @@ rb_secure(2); - if (getrlimit(NUM2INT(resource), &rlim) < 0) { + if (getrlimit(NUM2INT(rb_get_const(rb_mProcess,resource)), &rlim) < 0) { rb_sys_fail("getrlimit"); } return rb_assoc_new(RLIM2NUM(rlim.rlim_cur), RLIM2NUM(rlim.rlim_max)); @@ -1996,7 +1996,7 @@ rlim.rlim_cur = NUM2RLIM(rlim_cur); rlim.rlim_max = NUM2RLIM(rlim_max); - if (setrlimit(NUM2INT(resource), &rlim) < 0) { + if (setrlimit(NUM2INT(rb_get_const(rb_mProcess,resource)), &rlim) < 0) { rb_sys_fail("setrlimit"); } return Qnil; @@ -3585,7 +3585,6 @@ VALUE rb_mProcGID; VALUE rb_mProcID_Syscall; - /* * The Process module is a collection of methods used to * manipulate processes. Index: variable.c =================================================================== RCS file: /src/ruby/variable.c,v retrieving revision 1.140 diff -u -r1.140 variable.c --- variable.c 4 Sep 2006 05:46:47 -0000 1.140 +++ variable.c 12 Sep 2006 09:51:19 -0000 @@ -1340,6 +1340,54 @@ return rb_const_get_0(klass, id, Qfalse, Qtrue, fallback); } +VALUE rb_get_const_at(VALUE klass,VALUE name){ + ID id; + switch (TYPE(name)) { + case T_STRING: + case T_SYMBOL: + id = rb_to_id(name); + if (!rb_is_const_id(id)) { + rb_name_error(id, "wrong constant name %s", rb_id2name(id)); + } + return rb_const_get_at(klass, id); + break; + default: + return name; + } + return Qnil;/*dummy*/ +} +VALUE rb_get_const(VALUE klass,VALUE name){ + ID id; + switch (TYPE(name)) { + case T_STRING: + case T_SYMBOL: + id = rb_to_id(name); + if (!rb_is_const_id(id)) { + rb_name_error(id, "wrong constant name %s", rb_id2name(id)); + } + return rb_const_get(klass, id); + break; + default: + return name; + } + return Qnil;/*dummy*/ +} +VALUE +rb_constantize(VALUE klass, VALUE name) +{ + if (rb_obj_is_kind_of(name,rb_cArray)){ + long i; + long res=0; + for (i=0;i obj @@ -1549,8 +1597,8 @@ mod_av_set(klass, id, val, Qtrue); } -void -rb_define_const(VALUE klass, const char *name, VALUE val) +static void +rb_define_const_old(VALUE klass, const char *name, VALUE val) { ID id = rb_intern(name); @@ -1563,6 +1611,20 @@ rb_const_set(klass, id, val); } + /*same as rb_define const but strips only first strip prefixes*/ +void +rb_define_const_n(VALUE klass, const char *name, VALUE val,int strip) +{ + int len=strlen(name),i; + rb_define_const_old(klass,name,val); + for (i=0;i