Concepts of Programming Languages

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

the parent class seems to be a cleaner solution than the friend functions and
classes of C++.
The inclusion in C++ of constructors and destructors for initialization of
objects is good, but Ada 95 includes no such capabilities.
Another difference between these two languages is that the designer of a
C++ root class must decide whether a particular member function will be stati-
cally or dynamically bound. If the choice is made in favor of static binding, but
a later change in the system requires dynamic binding, the root class must be
changed. In Ada 95, this design decision need not be made with the design of
the root class. Each call can itself specify whether it will be statically or dynami-
cally bound, regardless of the design of the root class.

12.10 Support for Object-Oriented Programming in Ruby


As stated previously, Ruby is a pure object-oriented programming language in
the sense of Smalltalk. Virtually everything in the language is an object and all
computation is accomplished through message passing. Although programs have
expressions that use infix operators and therefore have the same appearance as
expressions in languages like Java, those expressions actually are evaluated through
message passing. As is the case with Smalltalk, when one writes a + b, it is exe-
cuted as sending the message + to the object referenced by a, passing a reference
to the object b as a parameter. In other words, a + b is implemented as a.+ b.

12.10.1 General Characteristics


Ruby class definitions differ from those of languages such as C++ and Java
in that they are executable. Because of this, they are allowed to remain open
during execution. A program can add members to a class any number of times,
simply by providing secondary definitions of the class that include the new
members. During execution, the current definition of a class is the union of
all definitions of the class that have been executed. Method definitions are
also executable, which allows a program to choose between two versions of a
method definition during execution, simply by putting the two definitions in
the then and else clause of a selection construct.
All variables in Ruby are references to objects, and all are typeless. Recall
that the names of all instance variables in Ruby begin with an at sign (@).
In a clear departure from the other common programming languages,
access control in Ruby is different for data than it is for methods. All instance
data has private access by default, and that cannot be changed. If external access
to an instance variable is required, accessor methods must be defined. For
example, consider the following skeletal class definition:

class MyClass

# A constructor
Free download pdf