зеркало из https://github.com/github/ruby.git
Iterate the documentation of Module.const_missing
This commit is contained in:
Родитель
9284fe123e
Коммит
077ac25ed8
25
variable.c
25
variable.c
|
@ -2300,23 +2300,28 @@ rb_const_missing(VALUE klass, VALUE name)
|
||||||
*
|
*
|
||||||
* Foo::UNDEFINED_CONST #=> :UNDEFINED_CONST: symbol returned
|
* Foo::UNDEFINED_CONST #=> :UNDEFINED_CONST: symbol returned
|
||||||
*
|
*
|
||||||
* In the next example when a reference is made to an undefined constant,
|
* As the example above shows, +const_missing+ is not required to create the
|
||||||
* it attempts to load a file whose name is the lowercase version of the
|
* missing constant in <i>mod</i>, though that is often a side-effect. The
|
||||||
* constant (thus class <code>Fred</code> is assumed to be in file
|
* caller gets its return value when triggered. If the constant is also defined,
|
||||||
* <code>fred.rb</code>). If found, it returns the loaded class. It
|
* further lookups won't hit +const_missing+ and will return the value stored in
|
||||||
* therefore implements an autoload feature similar to Kernel#autoload and
|
* the constant as usual. Otherwise, +const_missing+ will be invoked again.
|
||||||
* Module#autoload.
|
*
|
||||||
|
* 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)
|
* def Object.const_missing(name)
|
||||||
* @looked_for ||= {}
|
* @looked_for ||= {}
|
||||||
* str_name = name.to_s
|
* 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
|
* @looked_for[str_name] = 1
|
||||||
* file = str_name.downcase
|
* file = str_name.downcase
|
||||||
* require file
|
* require file
|
||||||
* klass = const_get(name)
|
* const_get(name, false)
|
||||||
* return klass if klass
|
|
||||||
* raise "Class not found: #{name}"
|
|
||||||
* end
|
* end
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
Загрузка…
Ссылка в новой задаче