Concepts of Programming Languages

(Sean Pound) #1

500 Chapter 11 Abstract Data Types and Encapsulation Constructs


class myClass
def meth1

...
end
end


This class could be extended by adding a second method, meth2, with a second
class definition:

class myClass
def meth2

...
end
end


Methods can also be removed from a class. This is done by providing
another class definition in which the method to be removed is sent to the
method remove_method as a parameter. The dynamic classes of Ruby are
another example of a language designer trading readability (and as a conse-
quence, reliability) for flexibility. Allowing dynamic changes to classes clearly
adds flexibility to the language, while harming readability. To determine the
behavior of a class at a particular point in a program, one must find all of its
definitions in the program and consider all of them.

11.4.6.2 Information Hiding
Access control for methods in Ruby is dynamic, so access violations are detected
only during execution. The default method access is public, but it can also be
protected or private. There are two ways to specify the access control, both of
which use functions with the same names as the access levels, private and
public. One way is to call the appropriate function without parameters. This
resets the default access for subsequently defined methods in the class. For
example,

class MyClass
def meth1

...
end
...
private
def meth7
...
end
...
end # of class MyClass

Free download pdf