D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
Module
/
Filename :
const_missing-i.ri
back
Copy
U:RDoc::AnyMethod[iI"const_missing:ETI"Module#const_missing;TF:publico:RDoc::Markup::Document:@parts[ o:RDoc::Markup::Paragraph; [I"BInvoked when a reference is made to an undefined constant in ;FI"G<i>mod</i>. It is passed a symbol for the undefined constant, and ;FI"Ireturns a value to be used for that constant. For example, consider:;Fo:RDoc::Markup::BlankLine o:RDoc::Markup::Verbatim; [ I"!def Foo.const_missing(name) ;TI"1 name # return the constant name as Symbol ;TI" end ;TI" ;TI"CFoo::UNDEFINED_CONST #=> :UNDEFINED_CONST: symbol returned ;T:@format0o; ; [ I"OAs the example above shows, +const_missing+ is not required to create the ;FI"Mmissing constant in <i>mod</i>, though that is often a side-effect. The ;FI"Scaller gets its return value when triggered. If the constant is also defined, ;FI"Sfurther lookups won't hit +const_missing+ and will return the value stored in ;FI"Mthe constant as usual. Otherwise, +const_missing+ will be invoked again.;F@o; ; [I"MIn the next example, when a reference is made to an undefined constant, ;FI"Q+const_missing+ attempts to load a file whose path is the lowercase version ;FI"Qof the constant name (thus class <code>Fred</code> is assumed to be in file ;FI"Q<code>fred.rb</code>). If defined as a side-effect of loading the file, the ;FI"Rmethod returns the value stored in the constant. This implements an autoload ;FI"Rfeature similar to Kernel#autoload and Module#autoload, though it differs in ;FI"important ways.;F@o;; [I"$def Object.const_missing(name) ;TI" @looked_for ||= {} ;TI" str_name = name.to_s ;TI"D raise "Constant not found: #{name}" if @looked_for[str_name] ;TI"! @looked_for[str_name] = 1 ;TI" file = str_name.downcase ;TI" require file ;TI" const_get(name, false) ;TI"end;T; 0: @fileI" object.c;T:0@omit_headings_from_table_of_contents_below0I"&mod.const_missing(sym) -> obj ;F0[ I" (p1);T@3FI"Module;TcRDoc::NormalClass00