Concepts of Programming Languages

(Sean Pound) #1
12.10 Support for Object-Oriented Programming in Ruby 565

Class variables, which are specified by preceding their names with two at
signs (@@), are private to the class and its instances. That privacy cannot be
changed. Also, unlike global and instance variables, class variables must be
initialized before they are used.

12.10.2 Inheritance


Subclasses are defined in Ruby using the less-than symbol (<), rather than the
colon of C++. For example,

class MySubClass < BaseClass

One distinct thing about the method access controls of Ruby is that they
can be changed in a subclass, simply by calling the access control functions.
This means that two subclasses of a base class can be defined so that objects of
one of the subclasses can access a method defined in the base class, but objects
of the other subclass cannot. Also, this allows one to change the access of a
publicly accessible method in the base class to a privately accessible method in
the subclass. Such a subclass obviously cannot be a subtype.
Ruby modules provide a naming encapsulation that is often used to define
libraries of functions. Perhaps the most interesting aspect of modules, however,
is that their functions can be accessed directly from classes. Access to the module
in a class is specified with an include statement, such as

include Math

The effect of including a module is that the class gains a pointer to the
module and effectively inherits the functions defined in the module. In fact,
when a module is included in a class, the module becomes a proxy superclass
of the class. Such a module is a mixin.

12.10.3 Dynamic Binding


Support for dynamic binding in Ruby is the same as it is in Smalltalk. Variables
are not typed; rather, they are all references to objects of any class. So, all vari-
ables are polymorphic and all bindings of method calls to methods are dynamic.

12.10.4 Evaluation


Because Ruby is an object-oriented programming language in the purest sense,
its support for object-oriented programming is obviously adequate. However,
access control to class members is weaker than that of C++. Ruby does not
support abstract classes or interfaces, although mixins are closely related to
interfaces. Finally, in large part because Ruby is interpreted, its execution effi-
ciency is far worse than that of the compiled languages.
Free download pdf