summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-01-17 14:05:49 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-01-17 14:05:49 +0000
commitbc6c81509671c22e5e9f383a94cee31dd35c591f (patch)
tree56accbca45e4b21ec8c7cc435b3fe6c5741746c2
parentc93aad61c61f9b1d3c6e4c394be9ddf1949d5ff0 (diff)
downloadruby-bc6c81509671c22e5e9f383a94cee31dd35c591f.tar.gz
ruby-bc6c81509671c22e5e9f383a94cee31dd35c591f.tar.xz
ruby-bc6c81509671c22e5e9f383a94cee31dd35c591f.zip
* 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] git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@9841 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog12
-rw-r--r--object.c45
2 files changed, 43 insertions, 14 deletions
diff --git a/ChangeLog b/ChangeLog
index a13bbebba..d4a5d5d75 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Tue Jan 17 23:05:21 2006 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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 <usa@ruby-lang.org>
* win32/setup.mak (MAKE): workaround for nmake 8.
@@ -35,13 +41,13 @@ Fri Jan 13 19:26:15 2006 Hirokazu Yamamoto <ocean@m2.ccsnet.ne.jp>
Thu Jan 12 11:53:08 2006 Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp>
* 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 <m_seki@mva.biglobe.ne.jp>
- * 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 <akr@m17n.org>
diff --git a/object.c b/object.c
index 4c6667ac0..233b9ee13 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 <i>mod</i>.
*
* 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 <code>true</code> if a constant with the given name is
- * defined by <i>mod</i>.
+ * defined by <i>mod</i>, 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",