150 likes | 546 Views
Bryant Land CSCI 431 November 5, 2009. Ruby . General Characteristics. Ruby is a pure OOPL Ruby class and method definitions are executable All variables in ruby are typeless and references to objects
E N D
Bryant Land CSCI 431 November 5, 2009 Ruby
General Characteristics • Ruby is a pure OOPL • Ruby class and method definitions are executable • All variables in ruby are typeless and references to objects • All instance variables are by default private and can only be accessed by using user defined access methods
Access Controls • Public • Private • Protected • Instance variables use a single @ • While class variables use two @ signs and are private and must be initialized before they are used • The constructor for Ruby class all are named initialize • Public is the default access control used if not specified by the user in Ruby • Ruby uses public access methods called attribute readers and writers to get access to private variables • Encapsulation is achieved in Ruby by having instance variables private to the object and accessing them through public getter and setter accessor methods. i.e, attr_reader & attr_writer
Example of Access Controls 1 class ClassAccess def m1 #this method is public end protected def m2 #this method is protected end private def m3 #this method is private end end ca = ClassAccess.new ca.m1 #ca.m2 #ca.m3
Example of Access Controls 1 Continued class ClassAccess def m1 #this method is public end public :m1 protected :m2, :m3 private :m4, :m5 end
Example of Access Controls 2 class Person def initialize(age) @age = age end def age @age end def compare_age(c) if c.age > age “The other object’s age is bigger.” else “The other object’s age is the same or smaller.” end end protected :age end
Example of Access Controls 2 Continued chris = Person.new(25) marcos = Person.new(34) puts chris.compare_age(marcos) #puts chris.age The output is: The other object’s age is bigger. If you were to remove the last comment you would get an error exception thrown by Ruby
Example of Access Controls 3 #First is defined without accessor methods Class Song def initialize(name, artist) @name = name @artist = artist end def name @name end def artist @artist end end song = Song.new(“Crawling”, “Linkin Park”) puts song.name puts song.artist
Example of Access Controls 3 Continued #Now defined with accessor methods Class Song def initialize(name, artist) @name = name @artist = artist end attr_reader :name, :artist #creates reader only # For creating attribute reader and writer methods # attr_accessor :name # For creating writer methods # attr_writer :name End song = Song.new(“In the End”, “Linkin Park”) puts song.name puts song.artist
Inheritance • Subclasses are defined in Ruby using the less than symbol (<) • Child (or subclass) < Parent (or base class) • Subclasses in ruby are !== subtypes • The subclass method access controls in ruby can be changed by simply calling the access control functions • Ruby uses modules as part of a naming encapsulation • Access to modules in a class can be accomplished by using the include statement
Inheritance Example 1 Class Dog def initialize (breed) @breed = breed end end Class Lab < Dog def initialize (breed, name) super(breed) @name = name end def to_s “(#@breed, #@name)” end end Puts Lab.new(“Labrador”, “Benzy”).to_s (Labrador, Benzy)
Inheritance Example 2 Class Bird def preen puts “I am cleaning my feathers.” end def fly puts “I am flying.” end end Class Penguin < Bird def fly puts “Sorry. I would rather swim.” end end p = Penguin.new p.preen I am cleaning my feathers. p.fly Sorry. I would rather swim.
Dynamic Binding • Variables are not typed they are references to objects of the class • All variables are polymorphic and all bindings of methods calls to methods are dynamic • Type checking is dynamic because they can be changed during execution and is usually implied upon being initialized by a specific value or assignment statement
Evaluation • Ruby provides adequate support for OOP • Its access controls are weaker than that of C++ • Multiple- inheritance is not supported • Ruby does not support abstract classes or interfaces but by making use of mix-ins it works similar to the interface • Mix-ins are accomplished through the use of modules being incorporated into the class methods and being used in various methods or different classes