diff --git a/ChangeLog b/ChangeLog index a13bbebba3..d4a5d5d75c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Tue Jan 17 23:05:21 2006 Nobuyoshi Nakada + + * object.c (rb_mod_const_get, rb_mod_const_defined): added optional + flag to search ancestors, which is defaulted to true. + fixed: [ruby-talk:175899] + Tue Jan 17 11:31:47 2006 NAKAMURA Usaku * win32/setup.mak (MAKE): workaround for nmake 8. @@ -35,13 +41,13 @@ Fri Jan 13 19:26:15 2006 Hirokazu Yamamoto Thu Jan 12 11:53:08 2006 Hidetoshi NAGAI * ext/tk/sample/tkballoonhelp.rb: [bug fix] couldn't add to a widget - which is constructed with TkComposite module. - [new feature] support 'command' option which is called just before + which is constructed with TkComposite module. + [new feature] support 'command' option which is called just before popping up the balloon help. Wed Jan 11 00:12:29 2006 Masatoshi SEKI - * lib/erb.rb (ERB::Compiler): add instance variable @insert_cmd to + * lib/erb.rb (ERB::Compiler): add instance variable @insert_cmd to change <%='s behavior. Tue Jan 10 19:42:33 2006 Tanaka Akira diff --git a/object.c b/object.c index 4c6667ac05..233b9ee131 100644 --- a/object.c +++ b/object.c @@ -1628,22 +1628,34 @@ rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass) /* * call-seq: - * mod.const_get(sym) => obj + * mod.const_get(sym, inherit=true) => obj * * Returns the value of the named constant in mod. * * Math.const_get(:PI) #=> 3.14159265358979 + * + * If the constant is not defined or is defined by the ancestors and + * +inherit+ is false, +NameError+ will be raised. */ static VALUE -rb_mod_const_get(VALUE mod, VALUE name) +rb_mod_const_get(int argc, VALUE *argv, VALUE mod) { - ID id = rb_to_id(name); + VALUE name, recur; + ID id; + if (argc == 1) { + name = argv[0]; + recur = Qtrue; + } + else { + rb_scan_args(argc, argv, "11", &name, &recur); + } + 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(mod, id); + return RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id); } /* @@ -1672,23 +1684,34 @@ rb_mod_const_set(VALUE mod, VALUE name, VALUE value) /* * call-seq: - * mod.const_defined?(sym) => true or false + * mod.const_defined?(sym, inherit=true) => true or false * * Returns true if a constant with the given name is - * defined by mod. + * defined by mod, or its ancestors if +inherit+ is not false. * * Math.const_defined? "PI" #=> true + * IO.const_defined? "SYNC" #=> true + * IO.const_defined? "SYNC", false #=> false */ static VALUE -rb_mod_const_defined(VALUE mod, VALUE name) +rb_mod_const_defined(int argc, VALUE *argv, VALUE mod) { - ID id = rb_to_id(name); + VALUE name, recur; + ID id; + if (argc == 1) { + name = argv[0]; + recur = Qtrue; + } + else { + rb_scan_args(argc, argv, "11", &name, &recur); + } + 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_defined_at(mod, id); + return RTEST(recur) ? rb_const_defined(mod, id) : rb_const_defined_at(mod, id); } /* @@ -2498,9 +2521,9 @@ Init_Object(void) rb_class_private_instance_methods, -1); /* in class.c */ rb_define_method(rb_cModule, "constants", rb_mod_constants, 0); /* in variable.c */ - rb_define_method(rb_cModule, "const_get", rb_mod_const_get, 1); + rb_define_method(rb_cModule, "const_get", rb_mod_const_get, -1); rb_define_method(rb_cModule, "const_set", rb_mod_const_set, 2); - rb_define_method(rb_cModule, "const_defined?", rb_mod_const_defined, 1); + rb_define_method(rb_cModule, "const_defined?", rb_mod_const_defined, -1); rb_define_private_method(rb_cModule, "remove_const", rb_mod_remove_const, 1); /* in variable.c */ rb_define_method(rb_cModule, "const_missing",