This chapter demonstrates about the Ruby Object Oriented Ruby is an pure Object Oriented Programming Language in which every thing is appear to ruby as an object and following are the concepts covered in this chapter.
Ruby Class and Objects
Ruby Instance Methods
Ruby Class Methods and Variables
Access Control
Class Inheritance
Class Constants
Ruby Class and Objects
Description
Class define blue print of a data type actually which is not used to define any data but which defines class name means i.e what the class name consist and what operations it will performs on object. In class definition which is starts with class key word and precede by its class name and its is delimited with end the snippet below demonstrates the class as shown below.
[ruby]
class Box
code
end
[/ruby]
Class provides blue print for Objects basically class contain several objects so objects are created from classes user can define the objects with new keyword the snippet below demonstrates the ruby objects as shown below.
[ruby]
box1 = Box.new
box2 = Box.new
[/ruby]
Ruby Instance Methods
Description
Ruby instance methods are defined same as the normal methods by using the def keyword which is used as class instance and the functionality is not limited only for instance variables which can do a lots as per the user requirement. the code below demonstrates the ruby instance methods as shown below.
[ruby]
# define a class
class Box
# constructor method
def initialize(w,h)
@width, @height = w, h
end
# instance method
def getArea
@width * @height
end
end
# create an object
box = Box.new(10, 20)
# call instance methods
a = box.getArea()
puts "Area of the box is : #{a}"
[/ruby]
Result
By running the above code in command prompt, the output can be obtained as shown in the image below.
Ruby Class Methods and Variables
Description
class variables are similar to the variables which share between all instances of class. Class variable prefixed with two @ characters and class is defined with class definition.
def self.methodname() is used to define method of the class which ends with a delimiter and it should be called with class name i.e, classname.methodname as shown in below code.
[ruby]
class Box
# Initialize our class variables
@@count = 0
def initialize(w,h)
# assign instance avriables
@width, @height = w, h
@@count += 1
end
def self.printCount()
puts "Box count is : #@@count"
end
end
[/ruby]
Access Control
Description
Ruby have three methods of protection to the instance methods those are the public, private and protected. Ruby doesn't have any access controls on class variables, instances.
Public
Public methods these are accessed by any public methods by default except for initialise.
Private
Private methods are cannot accessed by every one or even saw from the out side of the class. Only class methods can be accessed by private methods.
Protected
Protected method which is accessed by only objects of the its class and its sub classes. Which is accessible by only with in the family.
The code below demonstrates the three access modifiers as shown.
[ruby]
# define a class
class Box
# constructor method
def initialize(w,h)
@width, @height = w, h
end
# instance method by default it is public
def getArea
getWidth() * getHeight
end
# define private accessor methods
def getWidth
@width
end
def getHeight
@height
end
# make them private
private :getWidth, :getHeight
# instance method to print area
def printArea
@area = getWidth() * getHeight
puts "Big box area is : #@area"
end
# make it protected
protected :printArea
end
# create an object
box = Box.new(10, 20)
# call instance methods
a = box.getArea()
puts "Area of the box is : #{a}"
# try to call protected or methods
box.printArea()
[/ruby]
Result
By running the above code in command prompt, the output can be obtained as shown in the image below.
Class Inheritance
Description
Inheritance provides rescue the code functionality and improves fast and best implementation of code but ruby doesn't support the multiple inheritance but which support mixins which means sepecialized implementation of multiple inheritance. The code below demonstrates the class Inheritance as shown below.
[ruby]
# define a class
class Box
# constructor method
def initialize(w,h)
@width, @height = w, h
end
# instance method
def getArea
@width * @height
end
end
# define a subclass
class BigBox < Box
# add a new instance method
def printArea
@area = @width * @height
puts "Big box area is : #@area"
end
end
# create an object
box = BigBox.new(10, 20)
# print the area
box.printArea()
[/ruby]
Result
By running the above code in command prompt, the output can be obtained as shown in the image below.