<html> <head> <title>Ruby FAQ: Classes and modules </title> </head> <body> <a href="rubyfaq-5.html">Previous</a> <a href="rubyfaq-7.html">Next</a> <a href="rubyfaq.html#toc6">Table of contents</a> <hr> <h2><a name="s6">6. Classes and modules </a></h2> <h2><a name="ss6.1">6.1 Can a class definition be repeated? </a></h2> A class can be defined repeatedly and the definition is added to the former definition. If a method is redefined, the former one is overridden and lost. <h2><a name="ss6.2">6.2 Is there a class variable? </a></h2> Ruby does not have a class variable. But container classes (<code>Array</code>, <code>Hash</code>, etc) assigned to a class constant can be used as a class variable. <blockquote><code><pre> class Foo F = [0] def foo F[0] += 1 print F[0], "\n" end end </pre></code></blockquote> <h2><a name="ss6.3">6.3 What is a class instance variable? </a></h2> <blockquote><code><pre> class Foo @a = 123 # (1) def foo p @a # (2) ... nil not 123 end end </pre></code></blockquote> (1) is a class instance variable, and (2) is an ordinary instance variable. (2) belongs to an instance of the class <code>Foo</code>, and (1) belongs to the class object <code>Foo</code>, which is an instance of <code>Class</code> class. <br><br> There is no way to access the class instance variable from the instance methods. <br><br> (2) is an uninitialized instance variable with the value <code>nil</code>. <a name="singleton_method"></a><h2><a name="ss6.4">6.4 What is a singleton method? </a></h2> A singleton method is the proper method of a specified object. <br><br> It is used like this. <blockquote><code><pre> foo = Foo.new def foo.hello print "Hello\n" end foo.hello </pre></code></blockquote> It is useful when you want to add a method to an object but hesitate to define a new subclass. <br><br> Those who are familiar with Java may think it similar to an anonymous inner class. <h2><a name="ss6.5">6.5 Is there a class method? </a></h2> A singleton method of a class is called a class method. A singleton method is described as a proper method of an object, but there is a characteristic in Ruby which is a metaclass, and every class has a metaclass of the same name which is an instance of Class class. A class method is defined in the metaclass. <br><br> Formally, a class method is a method whose receiver is a class. <br><br> Let's look at a singleton method of <code>Foo</code> which is an instance of <code>Class</code>. <blockquote><code><pre> class Foo def Foo.test print "this is foo\n" end end </pre></code></blockquote> It is invoked this way. <blockquote><code><pre> Foo.test </pre></code></blockquote> This is a class method. Methods which are defined in <code>Class</code> can be used as class methods for every class. <h2><a name="ss6.6">6.6 What is a singleton class? </a></h2> A singleton class is a mechanism that enables various class operations to an object. <blockquote><code><pre> class Foo def hello print "hello.\n" end end foo = Foo.new foo.hello # -> hello. class << foo attr :name, TRUE def hello print "hello. I'm ", @name, ".\n" end end foo.name = "Tom" foo.hello # -> hello. I'm Tom. </pre></code></blockquote> Wow, we can add anything to an object. <br><br> Here is a quiz for you. <br><br> <b>Q.</b> How can you make a class method private without using <code>private_class_method</code>? <br><br> The answer is as follows. <blockquote><code><pre> class Foo ... end class << Foo def class_method print "class method\n" end private :class_method end Foo.class_method " # -> Error </pre></code></blockquote> There are two ways in defining a singleton method, one is to define in a singleton class, and the other is to define directly using the form def obj.method. <br><br> In a module, you can define a singleton method (at the same time a private method) by making a method a module function. <h2><a name="ss6.7">6.7 What is a module function? </a></h2> A method in a module which is defined as a singleton method as well as a private method is called a module function. For example: <blockquote><code><pre> Math.sqrt(2) </pre></code></blockquote> Or can be used with <code>include</code>. <blockquote><code><pre> include Math sqrt(2) </pre></code></blockquote> To make a method a module function, you invoke module_function method. <blockquote><code><pre> module_function :method_name </pre></code></blockquote> <h2><a name="ss6.8">6.8 What is the difference between a class and a module? </a></h2> A module cannot generate an instance. A class cannot be included. <h2><a name="ss6.9">6.9 Does a module generate a subclass? </a></h2> A module is included in a class/module to mimic multiple inheritance (Mix-in). This does not generate a subclass which is a direct inheritance, but the class has is_a? relation with the module. <h2><a name="ss6.10">6.10 What is the difference between defining a class method in the class definition and directly at the toplevel? </a></h2> In the class definition you can directly access a constant. At the toplevel a constant is accessed using the class name dereference. <h2><a name="ss6.11">6.11 What is the difference between <code>load</code> and <code>require</code>? </a></h2> Only a Ruby script (<code>*.rb</code>) is loaded and executed in <code>load</code>. <br><br> When <code>require</code>d, <code>*.o</code> file is also looked for. An already <code>require</code>d file is never loaded again. <br><br> The loading path is different. <h2><a name="ss6.12">6.12 What is the difference between <code>include</code> and <code>extend</code>? </a></h2> <code>include</code> is used to include a module to a class/module, and methods in the module are called in function-style. <code>extend</code> is used to include a module to an object(instance), and methods in the module become singleton methods of the object. <h2><a name="ss6.13">6.13 What does <code>self</code> mean? </a></h2> <code>self</code> means the object itself to which a method is applied. A function form method call implies <code>self</code> as the receiver. <hr> <a href="rubyfaq-5.html">Previous</a> <a href="rubyfaq-7.html">Next</a> <a href="rubyfaq.html#toc6">Table of contents</a> </body> <html>