diff options
author | Xavier Noria <[email protected]> | 2024-03-09 14:00:02 +0100 |
---|---|---|
committer | Nobuyoshi Nakada <[email protected]> | 2024-03-15 22:21:43 +0900 |
commit | 077ac25ed8c699724467f209cf8df84b4abf9768 (patch) | |
tree | ed1238e8c7740e165cbedffd192ef53128eb378b /variable.c | |
parent | 9284fe123ef583ec4ba09097bcc795a54b1f5cf7 (diff) |
Iterate the documentation of Module.const_missing
Diffstat (limited to 'variable.c')
-rw-r--r-- | variable.c | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/variable.c b/variable.c index 4e2ac0dcdc..807f01d64f 100644 --- a/variable.c +++ b/variable.c @@ -2300,23 +2300,28 @@ rb_const_missing(VALUE klass, VALUE name) * * Foo::UNDEFINED_CONST #=> :UNDEFINED_CONST: symbol returned * - * In the next example when a reference is made to an undefined constant, - * it attempts to load a file whose name is the lowercase version of the - * constant (thus class <code>Fred</code> is assumed to be in file - * <code>fred.rb</code>). If found, it returns the loaded class. It - * therefore implements an autoload feature similar to Kernel#autoload and - * Module#autoload. + * As the example above shows, +const_missing+ is not required to create the + * missing constant in <i>mod</i>, though that is often a side-effect. The + * caller gets its return value when triggered. If the constant is also defined, + * further lookups won't hit +const_missing+ and will return the value stored in + * the constant as usual. Otherwise, +const_missing+ will be invoked again. + * + * In the next example, when a reference is made to an undefined constant, + * +const_missing+ attempts to load a file whose path is the lowercase version + * of the constant name (thus class <code>Fred</code> is assumed to be in file + * <code>fred.rb</code>). If defined as a side-effect of loading the file, the + * method returns the value stored in the constant. This implements an autoload + * feature similar to Kernel#autoload and Module#autoload, though it differs in + * important ways. * * def Object.const_missing(name) * @looked_for ||= {} * str_name = name.to_s - * raise "Class not found: #{name}" if @looked_for[str_name] + * raise "Constant not found: #{name}" if @looked_for[str_name] * @looked_for[str_name] = 1 * file = str_name.downcase * require file - * klass = const_get(name) - * return klass if klass - * raise "Class not found: #{name}" + * const_get(name, false) * end * */ |