summaryrefslogtreecommitdiff
path: root/object.c
diff options
context:
space:
mode:
authortenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-10-26 21:31:23 +0000
committertenderlove <tenderlove@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-10-26 21:31:23 +0000
commit82af182e740bcb2a3aa1d9b2b62412f8ab28a8b1 (patch)
treee5d30034648aaa2aafedd671029c8e19834872fb /object.c
parent8ee4f0ad80720d38022b81b1cead87fe4c08f006 (diff)
* object.c (rb_mod_const_get): const_get accepts qualified constant
strings. e.g. Object.const_get("Foo::Bar::Baz") [ruby-core:41404] * test/ruby/test_module.rb: tests for new behavior git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37335 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'object.c')
-rw-r--r--object.c97
1 files changed, 79 insertions, 18 deletions
diff --git a/object.c b/object.c
index eec42bf4d1..a7eccb8ced 100644
--- a/object.c
+++ b/object.c
@@ -1826,9 +1826,38 @@ rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
return Qnil;
}
+static VALUE
+rb_mod_single_const_get(VALUE mod, VALUE name, VALUE recur)
+{
+ ID id;
+
+ id = rb_check_id(&name);
+ if (!id) {
+ if (!rb_is_const_name(name)) {
+ rb_name_error_str(name, "wrong constant name %s", RSTRING_PTR(name));
+ }
+ else if (!rb_method_basic_definition_p(CLASS_OF(mod), id_const_missing)) {
+ id = rb_to_id(name);
+ }
+ else if (mod && rb_class_real(mod) != rb_cObject) {
+ rb_name_error_str(name, "uninitialized constant %s::%s",
+ rb_class2name(mod),
+ RSTRING_PTR(name));
+ }
+ else {
+ rb_name_error_str(name, "uninitialized constant %s", RSTRING_PTR(name));
+ }
+ }
+ if (!rb_is_const_id(id)) {
+ rb_name_error(id, "wrong constant name %s", rb_id2name(id));
+ }
+ return RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id);
+}
+
/*
* call-seq:
* mod.const_get(sym, inherit=true) -> obj
+ * mod.const_get(str, inherit=true) -> obj
*
* Checks for a constant with the given name in <i>mod</i>
* If +inherit+ is set, the lookup will also search
@@ -1838,12 +1867,33 @@ rb_mod_attr_accessor(int argc, VALUE *argv, VALUE klass)
* otherwise a +NameError+ is raised.
*
* Math.const_get(:PI) #=> 3.14159265358979
+ *
+ * This method will recursively look up constant names if a namespaced
+ * class name is provided. For example:
+ *
+ * module Foo; class Bar; end end
+ * Object.const_get 'Foo::Bar'
+ *
+ * The +inherit+ flag is respected on each lookup. For example:
+ *
+ * module Foo
+ * class Bar
+ * VAL = 10
+ * end
+ *
+ * class Baz < Bar; end
+ * end
+ *
+ * Object.const_get 'Foo::Baz::VAL' # => 10
+ * Object.const_get 'Foo::Baz::VAL', false # => NameError
*/
static VALUE
rb_mod_const_get(int argc, VALUE *argv, VALUE mod)
{
VALUE name, recur;
+ rb_encoding *enc;
+ const char *pbeg, *p, *path;
ID id;
if (argc == 1) {
@@ -1853,27 +1903,38 @@ rb_mod_const_get(int argc, VALUE *argv, VALUE mod)
else {
rb_scan_args(argc, argv, "11", &name, &recur);
}
- id = rb_check_id(&name);
- if (!id) {
- if (!rb_is_const_name(name)) {
- rb_name_error_str(name, "wrong constant name %s", RSTRING_PTR(name));
- }
- else if (!rb_method_basic_definition_p(CLASS_OF(mod), id_const_missing)) {
- id = rb_to_id(name);
- }
- else if (mod && rb_class_real(mod) != rb_cObject) {
- rb_name_error_str(name, "uninitialized constant %s::%s",
- rb_class2name(mod),
- RSTRING_PTR(name));
+
+ if (SYMBOL_P(name)) {
+ name = rb_sym_to_s(name);
+ }
+
+ enc = rb_enc_get(name);
+ path = RSTRING_PTR(name);
+
+ if (!rb_enc_asciicompat(enc)) {
+ rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)");
+ }
+
+ pbeg = p = path;
+ while (*p) {
+ while (*p && *p != ':') p++;
+ id = rb_intern3(pbeg, p-pbeg, enc);
+ if (p[0] == ':') {
+ if (p[1] != ':') {
+ rb_raise(rb_eArgError, "undefined class/module %.*s", (int)(p-path), path);
+ }
+ p += 2;
+ pbeg = p;
}
- else {
- rb_name_error_str(name, "uninitialized constant %s", RSTRING_PTR(name));
+
+ if (!RB_TYPE_P(mod, T_MODULE) && !RB_TYPE_P(mod, T_CLASS)) {
+ rb_raise(rb_eTypeError, "%s does not refer to class/module", path);
}
+
+ mod = rb_mod_single_const_get(mod, ID2SYM(id), recur);
}
- if (!rb_is_const_id(id)) {
- rb_name_error(id, "wrong constant name %s", rb_id2name(id));
- }
- return RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id);
+
+ return mod;
}
/*