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

ruby-changes:26659

From: drbrain <ko1@a...>
Date: Sun, 6 Jan 2013 05:35:42 +0900 (JST)
Subject: [ruby-changes:26659] drbrain:r38710 (trunk): * doc/syntax/modules_and_classes.rdoc: Added singleton classes

drbrain	2013-01-06 05:35:30 +0900 (Sun, 06 Jan 2013)

  New Revision: 38710

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

  Log:
    * doc/syntax/modules_and_classes.rdoc:  Added singleton classes
      documentation.

  Modified files:
    trunk/ChangeLog
    trunk/doc/syntax/modules_and_classes.rdoc

Index: doc/syntax/modules_and_classes.rdoc
===================================================================
--- doc/syntax/modules_and_classes.rdoc	(revision 38709)
+++ doc/syntax/modules_and_classes.rdoc	(revision 38710)
@@ -291,3 +291,48 @@ provide them manually like <code>super(2 https://github.com/ruby/ruby/blob/trunk/doc/syntax/modules_and_classes.rdoc#L291
 
 +super+ may be called as many times as you like in the subclass method.
 
+= Singleton Classes
+
+The singleton class (also known as the metaclass or eigenclass) of an object is
+a class that holds methods for only that instance.  You can access the
+singleton class of an object using <code>class << object</code> like this:
+
+  class C
+  end
+
+  class << C
+    # self is the singleton class here
+  end
+
+Most frequently you'll see the singleton class accessed like this:
+
+  class C
+    class << self
+      # ...
+    end
+  end
+
+This allows definition of methods and attributes on a class (or module) without
+needing to write <code>def self.my_method</code>.
+
+Since you can open the singleton class of any object this means that this code
+block:
+
+  o = Object.new
+
+  def o.my_method
+    1 + 1
+  end
+
+is equivalent to this code block:
+
+  o = Object.new
+
+  class << o
+    def my_method
+      1 + 1
+    end
+  end
+
+Both objects with have a +my_method+ that returns +2+.
+
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 38709)
+++ ChangeLog	(revision 38710)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Jan  6 05:35:18 2013  Eric Hodel  <drbrain@s...>
+
+	* doc/syntax/modules_and_classes.rdoc:  Added singleton classes
+	  documentation.
+
 Sun Jan  6 02:22:00 2013  Zachary Scott  <zachary@z...>
 
 	* lib/webrick/httpservlet/abstract.rb (WEBrick::HTTPServlet): Typo in

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

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