[前][次][番号順一覧][スレッド一覧]

ruby-changes:20073

From: drbrain <ko1@a...>
Date: Thu, 16 Jun 2011 15:19:15 +0900 (JST)
Subject: [ruby-changes:20073] drbrain:r32120 (trunk): * variable.c (const_missing): Add simple example of const_missing.

drbrain	2011-06-16 15:17:59 +0900 (Thu, 16 Jun 2011)

  New Revision: 32120

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=32120

  Log:
    * variable.c (const_missing):  Add simple example of const_missing.
      Patch by Anuj Dutta.  [Ruby 1.9 - Bug #4794]

  Modified files:
    trunk/ChangeLog
    trunk/variable.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 32119)
+++ ChangeLog	(revision 32120)
@@ -1,3 +1,8 @@
+Thu Jun 16 15:17:39 2011  Eric Hodel  <drbrain@s...>
+
+	* 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@s...>
 
 	* lib/monitor.rb:  Improve documentation.  Patch by Sandor Szucs.
Index: variable.c
===================================================================
--- variable.c	(revision 32119)
+++ variable.c	(revision 32120)
@@ -1370,28 +1370,36 @@
  * 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
+ *
  */
 
 VALUE

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]