Method: Module#const_get

Defined in:
object.c

#const_get(sym, inherit = true) ⇒ Object #const_get(str, inherit = true) ⇒ Object

Checks for a constant with the given name in mod. If inherit is set, the lookup will also search the ancestors (and Object if mod is a Module).

The value of the constant is returned if a definition is found, 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

If the argument is not a valid constant name a NameError will be raised with a warning “wrong constant name”.

Object.const_get ‘foobar’ #=> NameError: wrong constant name foobar

Overloads:

  • #const_get(sym, inherit = true) ⇒ Object

    Returns:

  • #const_get(str, inherit = true) ⇒ Object

    Returns:



2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
# File 'object.c', line 2090

static VALUE
rb_mod_const_get(int argc, VALUE *argv, VALUE mod)
{
    VALUE name, recur;
    rb_encoding *enc;
    const char *pbeg, *p, *path, *pend;
    ID id;

    rb_check_arity(argc, 1, 2);
    name = argv[0];
    recur = (argc == 1) ? Qtrue : argv[1];

    if (SYMBOL_P(name)) {
  if (!rb_is_const_sym(name)) goto wrong_name;
  id = rb_check_id(&name);
  if (!id) return rb_const_missing(mod, name);
  return RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id);
    }

    path = StringValuePtr(name);
    enc = rb_enc_get(name);

    if (!rb_enc_asciicompat(enc)) {
  rb_raise(rb_eArgError, "invalid class path encoding (non ASCII)");
    }

    pbeg = p = path;
    pend = path + RSTRING_LEN(name);

    if (p >= pend || !*p) {
      wrong_name:
  rb_name_error_str(name, "wrong constant name % "PRIsVALUE, name);
    }

    if (p + 2 < pend && p[0] == ':' && p[1] == ':') {
  mod = rb_cObject;
  p += 2;
  pbeg = p;
    }

    while (p < pend) {
  VALUE part;
  long len, beglen;

  while (p < pend && *p != ':') p++;

  if (pbeg == p) goto wrong_name;

  id = rb_check_id_cstr(pbeg, len = p-pbeg, enc);
  beglen = pbeg-path;

  if (p < pend && p[0] == ':') {
      if (p + 2 >= pend || p[1] != ':') goto wrong_name;
      p += 2;
      pbeg = p;
  }

  if (!RB_TYPE_P(mod, T_MODULE) && !RB_TYPE_P(mod, T_CLASS)) {
      rb_raise(rb_eTypeError, "%"PRIsVALUE" does not refer to class/module",
         QUOTE(name));
  }

  if (!id) {
      part = rb_str_subseq(name, beglen, len);
      OBJ_FREEZE(part);
      if (!ISUPPER(*pbeg) || !rb_is_const_name(part)) {
    rb_name_error_str(part, "wrong constant name %"PRIsVALUE,
          QUOTE(part));
      }
      else if (!rb_method_basic_definition_p(CLASS_OF(mod), id_const_missing)) {
    part = rb_str_intern(part);
    mod = rb_const_missing(mod, part);
    continue;
      }
      else {
    rb_name_error_str(part, "uninitialized constant %"PRIsVALUE"% "PRIsVALUE,
          rb_str_subseq(name, 0, beglen),
          part);
      }
  }
  if (!rb_is_const_id(id)) {
      rb_name_error(id, "wrong constant name %"PRIsVALUE,
        QUOTE_ID(id));
  }
  mod = RTEST(recur) ? rb_const_get(mod, id) : rb_const_get_at(mod, id);
    }

    return mod;
}