summaryrefslogtreecommitdiff
path: root/object.c
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
commitdd677b700c1a8a1017d3265450fb58e1be4b11d3 (patch)
tree56accbca45e4b21ec8c7cc435b3fe6c5741746c2 /object.c
parentdc2732c84f1005821904080543783f302002193a (diff)
* 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: svn+ssh://ci.ruby-lang.org/ruby/trunk@9841 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'object.c')
-rw-r--r--object.c45
1 files changed, 34 insertions, 11 deletions
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 <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",