






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Ruby is purely object oriented. Everything in Ruby is an object. Even numbers and strings are objects. Ruby’s inheritance hierarchy: Singly rooted hierarchy with the class Object being the superclass of all classes, similar to The following examples demonstrate the pure object-oriented nature of Ruby. Try this in the Interactive Ruby Browser: 15.modulo 4
3 3.14159.between? 3, 4 true [11, 12, 13, 14, 15].slice 2, 4 [13, 14, 15]
Let us define a Person class with attributes name and height. It has a class variable count. which keeps track of the number of instances of the class created. class Person @@count = 0 def initialize(name, height) @name = name @height = height @@count += 1 end def self.info puts "Number of instances = #{@@count}" end def to_s "Name: #{@name} Height: #{@height}" Lecture 6 Object-Oriented Languages and Systems 1
end end p = Person.new("Tom",170) p2 = Person.new("Harry",165) puts p puts p Person.info Looking at the program what can you say about naming syntax for variables and methods? To instantiate an object of the new class, we write varname = classname.new. This automatically calls the class’s initialize method. The arguments to the new method are passed to the initialize method. The initialize method may cause any component objects to be created. The instance variables have names beginning with Class variables have names beginning with Class methods are declared beginning with Whenever an object is printed its, to_s method is called. Note: While printing, values of variables in double-quoted strings may be accessed with #{varname).
In languages that provide information hiding, one usually defines getter and setter methods. Why? Ruby has shortcuts to define these. Since Ruby classes are open to extension, we can add accessors to the definition of the Person class we defined above. CSC/ECE 517 Lecture Notes © 2007 Edward F. Gehringer 2
In Ruby, the < operator indicates inheritance. Let’s define class Employee to inherit from the Person class defined above. class Employee < Person attr_accessor :salary def initialize(name, height, salary) super(name, height) @salary = salary end def to_s super + " Salary: #{@salary}" end end We add a new attribute salary to the Employee class. The initialize method first calls the initialize method of the parent class using super, and then sets the salary. Similarly the to_s method is overridden in the Employee class. What does super do? e = Employee.new("Rob", 180, 80000) puts e
Name: Rob Height: 180 Salary: 80000
Ruby provides three levels of access control – Public methods can be called by anyone—no access control is enforced. Methods are public by default (except for , which is always private). Protected methods can be invoked only by objects of the defining class and its subclasses. Access is kept within the family. CSC/ECE 517 Lecture Notes © 2007 Edward F. Gehringer 4
Private methods cannot be called with an explicit receiver—the receiver is always self. This means that private methods can be called only in the context of the current object; you can’t invoke another object’s private methods. By contrast, if a method is protected, it may be called by any instance of the defining class or its subclasses. Can you think of a case where a private method would need to be called by another object of the same class? If a method is private, it may be called only within the context of the calling object—it is never possible to access another object’s private methods directly, even if the object is of the same class as the caller. class MyClass def method1 # default is “public” #... end protected # subsequent methods will be “protected” def method2 # will be “protected” #... end private # subsequent methods will be “private” def method3 # will be “private” #... end public # subsequent methods will be “public” def method4 # and this will be “public” #... end end Lecture 6 Object-Oriented Languages and Systems 5
Ruby has dynamic type checking. A method can be invoked on a variable whenever the type of object assigned to the variable has that method defined on it. This means that if the parameter passed to a method supports the methods invoked on it by the called method, then the calls work. This is unbounded polymorphism , which can only be found in dynamically typed languages. What’s the typical rule in statically typed languages? The dynamic approach is also called “duck typing,” after the expression, “ If it walks like a duck and talks like a duck then it must be a duck ”. Here is some code illustrating the concept: class Duck def quack puts "Quack!" end def walk puts "Waddle" end end class Toy def quack puts "Kwack!" end def walk class Pig def eat puts "I just eat!" end end class Test def test_duck(duck) duck.quack duck.walk end end t = Test.new t.test_duck(Duck.new) t.test_duck(Toy.new) t.test_duck(Pig.new) Lecture 6 Object-Oriented Languages and Systems 7
puts "Waggle" end end What is the result of executing this code? The test_duck method duck does not check the object type of the input argument that is passed in. So any object which has the methods quack and walk can be passed to the method and the methods called on it will be successfully executed. When we try to pass in an object lacking those methods, a NoMethodError is thrown at run time. Duck typing trades off safety to save lines of code. Any adverse impact on safety can be mitigated by carefully checking the code. Unbounded polymorphism allegedly makes it possible to add more functionality without affecting the existing design making one’s programs more flexible.
Ruby was invented as “a better Perl,” and hence has excellent built-in support for regular expressions. This is why it is useful as a scripting language. Here’s an example: "faculty".sub(/c/, "")
"cheese".gsub(/e/, "o")
CSC/ECE 517 Lecture Notes © 2007 Edward F. Gehringer 8
"choose".sub(/o+/, "e")
"choose".sub(/o*/, "e")
"choose".sub(/o?/, "e")
Note: Merely calling sub creates a new string object which cannot be modified until it has been assigned to a variable (str). This is similar to Java where all variables are references. Write Ruby code to remove single-line comments from a C program /* comments */ Write Ruby code to recognize dates entered in the format year-month-day (where “month” is a 3-letter abbreviation and years may either have 2 or 4 digits). CSC/ECE 517 Lecture Notes © 2007 Edward F. Gehringer 10