* variable.c (const_missing): Add simple example of const_missing.

Patch by Anuj Dutta.  [Ruby 1.9 - Bug #4794]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@32120 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
drbrain 2011-06-16 06:17:59 +00:00
parent 14d0f7aa48
commit 52607877c6
2 changed files with 33 additions and 20 deletions

View File

@ -1,3 +1,8 @@
Thu Jun 16 15:17:39 2011 Eric Hodel <drbrain@segment7.net>
* variable.c (const_missing): Add simple example of const_missing.
Patch by Anuj Dutta. [Ruby 1.9 - Bug #4794]
Thu Jun 16 15:09:29 2011 Eric Hodel <drbrain@segment7.net>
* lib/monitor.rb: Improve documentation. Patch by Sandor Szucs.

View File

@ -1370,27 +1370,35 @@ const_missing(VALUE klass, ID id)
* call-seq:
* mod.const_missing(sym) -> obj
*
* Invoked when a reference is made to an undefined constant in
* <i>mod</i>. It is passed a symbol for the undefined constant, and
* returns a value to be used for that constant. The
* following code is a (very bad) example: if 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
* value of the loaded class. It therefore implements a perverse
* kind of autoload facility.
* Invoked when a reference is made to an undefined constant in
* <i>mod</i>. It is passed a symbol for the undefined constant, and
* returns a value to be used for that constant. The
* following code is an example of the same:
*
* def Object.const_missing(name)
* @looked_for ||= {}
* str_name = name.to_s
* raise "Class 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}"
* end
* def Foo.const_missing(name)
* name # return the constant name as Symbol
* end
*
* 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.
*
* def Object.const_missing(name)
* @looked_for ||= {}
* str_name = name.to_s
* raise "Class 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}"
* end
*
*/