Previous Next Table of contents

6. Classes and modules

6.1 Can a class definition be repeated?

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.

6.2 Is there a class variable?

Ruby does not have a class variable. But container classes (Array, Hash, etc) assigned to a class constant can be used as a class variable.
class Foo
  F = [0]
  def foo
    F[0] += 1
    print F[0], "\n"
  end
end

6.3 What is a class instance variable?

class Foo
  @a = 123 # (1)
  def foo
    p @a  # (2) ... nil not 123
  end
end
(1) is a class instance variable, and (2) is an ordinary instance variable. (2) belongs to an instance of the class Foo, and (1) belongs to the class object Foo, which is an instance of Class class.

There is no way to access the class instance variable from the instance methods.

(2) is an uninitialized instance variable with the value nil.

6.4 What is a singleton method?

A singleton method is the proper method of a specified object.

It is used like this.
foo = Foo.new
def foo.hello
  print "Hello\n"
end
foo.hello
It is useful when you want to add a method to an object but hesitate to define a new subclass.

Those who are familiar with Java may think it similar to an anonymous inner class.

6.5 Is there a class method?

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.

Formally, a class method is a method whose receiver is a class.

Let's look at a singleton method of Foo which is an instance of Class.
class Foo
  def Foo.test
    print "this is foo\n"
  end
end
It is invoked this way.
Foo.test
This is a class method. Methods which are defined in Class can be used as class methods for every class.

6.6 What is a singleton class?

A singleton class is a mechanism that enables various class operations to an object.
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.
Wow, we can add anything to an object.

Here is a quiz for you.

Q. How can you make a class method private without using private_class_method?

The answer is as follows.
class Foo
  ...
end

class << Foo
  def class_method
    print "class method\n"
  end
  private :class_method
end
Foo.class_method " # -> Error
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.

In a module, you can define a singleton method (at the same time a private method) by making a method a module function.

6.7 What is a module function?

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:
Math.sqrt(2)
Or can be used with include.
include Math
sqrt(2)
To make a method a module function, you invoke module_function method.
module_function :method_name

6.8 What is the difference between a class and a module?

A module cannot generate an instance. A class cannot be included.

6.9 Does a module generate a subclass?

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.

6.10 What is the difference between defining a class method in the class definition and directly at the toplevel?

In the class definition you can directly access a constant. At the toplevel a constant is accessed using the class name dereference.

6.11 What is the difference between load and require?

Only a Ruby script (*.rb) is loaded and executed in load.

When required, *.o file is also looked for. An already required file is never loaded again.

The loading path is different.

6.12 What is the difference between include and extend?

include is used to include a module to a class/module, and methods in the module are called in function-style. extend is used to include a module to an object(instance), and methods in the module become singleton methods of the object.

6.13 What does self mean?

self means the object itself to which a method is applied. A function form method call implies self as the receiver.
Previous Next Table of contents