Abstract
The purpose of this document is to demonstrate how object oriented programming works with Ruby. In this document we would use inheritance, one of the most important concepts of object oriented paradigm.
Prerequisite:
You must have a Ruby interpreter installed on the machine.
Method/measurement:
Step1- Setup Foundations:
We will first try to understand the class hierarchy that we need to create through Ruby programming. In this example we will use mamalia, Bruta, Asian Elephant and West Indian Manatee mammals of the hierarchy and would explain how inheritance can be used in using Ruby. In the later chapter you would find one more example of class hierarchy Mammalia->Pecora->Dromedary->Sheep Let’s first identify some of the characteristics that every mammal has
Heart : 2 auricles, 2 ventricles. Warm, dark red blood
Lungs : respires alternately
Sense Organs : tongue, nostrils, eyes, ears, & papillae of the skin
Supports : 4 feet, except in aquatics; and in most a tail. Walks on the Earth & Speak
Conservation status: Generally has a conservation status.
Note : Even though every mammal has other common characteristics. We would use only subset of these characteristics for our example.
Bruta is another classification of Mammals which has characteristics such as:
Food : (mostly) masticated vegetables
Motion : slow
Note : Even though Bruta type mammal has other common characteristics. We would use only subset of these characteristics for our example.
Asian elephant falls under the category of Bruta and has following different characteristics:
Trunk : Asian Elephants have trunk :The distinctive trunk is an elongation of the nose and upper lip combined
Intelligence: Asian Elephants are highly intelligent and self-aware
Size : Largest Asian Elephant recorded weighed 8 tonnes , stood 3.35 m (11 ft) tall at the shoulders and was 8.06 m long from head to tail.
Color : Skin color is usually gray, and may be masked by soil because of dusting and wallowing.
Conservation Status : Endangered
West Indian Manatee :West Indian Manatee falls under the category of Bruta mammals and has following different characteristics
Size : The largest individual on record weighed 1,655 kg (3,649 lb) and measured 4.6 m (15 ft) long.
Color : Color is grey and brown
Aquatic: West indian Manatee is aquatic.
Conservation status : Vulnerable
Below is the pictorial Representation of the class hierarchy explained in the above section
Step 2-Creating classes in Ruby
Now let’s create the class hierarchy in Ruby .
This is base class for our program and contains following methods:
Initialize (class_hierarchical_pos="root class:Mammalia",status=""): Default constructor for the class
get_class_hierarchy_pos : This method returns the class hierarchy position
heart: This function returns the characteristic of heart.
Lungs : This function returns characteristics of lungs.
Senseorgan : This method returns characteristics of sense organs.
get_conservation_status (status="") : This function returns the conservation status of mammal depending on the passed parameter.
Supports : This function returns supports characterstics.
For the class syntax kindly check the source code at the end of this article.
Create Bruta class :This is Bruta Class ,which is Subclass of Mammalia(Mammalia->BRUTA) and contains following methods:
Initialize (class_hierarchical_pos="Bruta ,which is Subclass of Mammalia(Mammalia->BRUTA)"): Default constructor for the class
Food: This method returns the Food habits.
Motion: This function returns Motion Characteristics
Isaquatic: returns whether mammal is aquatic or not depending on parameter passed
Apart from the mentioned characterstics, bruta acquires following characteristics from the Mammalia class, implicitly, as it is inherited from Mamallia class.
get_class_hierarchy_pos : This method returns the class hierarchy position
heart: This function returns the characteristic of heart.
Lungs : This function returns characteristics of lungs.
Senseorgan : This method returns characteristics of sense organs.
get_conservation_status (status="") : This function returns the conservation status of mammal depending on the passed parameter.
Supports : This function returns supports characteristics.
For the class syntax kindly double click on the attached file:
Create Asian Elephant class :
This is Asian Elephant class , which is Subclass of Bruta(ROOT->BRUTA->Asian_Elephant) and contains following methods:
GetStatus: Returns the conservation status for Asian Elephant
Trunk : Returns Trunk characteristics of Asian elephant
Size : Returns largest recorded size of elephant
Color : Returns color of Asian elephant
Isaquatic : returns whether mammal is aquatic or not .
Apart from the mentioned characteristics, Asian Elephant following characteristics from the Bruta class, implicitly, as it is inherited from Bruta class.
Food: This method returns the Food habits.
Motion: This function returns Motion Characteristics
Isaquatic: returns whether mammal is aquatic or not depending on parameter passed
It also acquires following characteristics from Mammalia class, as it is inherited from Bruta class.
get_class_hierarchy_pos : This method returns the class hierarchy position
heart: This function returns the characteristic of heart.
Lungs : This function returns characteristics of lungs.
Senseorgan : This method returns characteristics of sense organs.
get_conservation_status (status="") : This function returns the conservation status of mammal depending on the passed parameter.
Supports : This function returns supports characteristics.
For the class syntax kindly double click on the attached file:
Create West_Indian_Manatee class :
This is West_Indian_Manatee Class , which is Subclass of Bruta(ROOT->BRUTA-> West_Indian_Manatee) and contains following methods:
GetStatus: Returns the conservation status for Asian Elephant
Size : Returns largest recorded size of elephant
Color : Returns color of Asian elephant
Isaquatic : returns whether mammal is aquatic or not .
Apart from the mentioned characteristics, West_Indian_Manatee aquires following characteristics from the Bruta class, implicitly , as it is inherited from Bruta class.
Food: This method returns the Food habits.
Motion: This function returns Motion Characteristics
Isaquatic: returns whether mammal is aquatic or not depending on parameter passed
It also acquires following characteristics from Mammalia class
get_class_hierarchy_pos : This method returns the class hierarchy position
heart: This function returns the characteristic of heart.
Lungs : This function returns characteristics of lungs.
Senseorgan : This method returns characteristics of sense organs.
get_conservation_status (status="") : This function returns the conservation status of mammal depending on the passed parameter.
Supports : This function returns supports characteristics.
For the class syntax kindly double click on the attached file:
Step 3-Write the Main program:
Now we will write the main program that would depict our class hierarchy and characteristics of our different objects.
# Create an object of Root class i.e Mammalia class and perform the actions
objmammalia=Mammalia.new
objmammalia.get_class_hierarchy_pos()
puts " Characteristics of Mammalia:"
objmammalia.heart()
objmammalia.lungs()
objmammalia.senseorgan()
objmammalia.Supports()
# Create an object of Bruta Class and perform the actions
objbruta=Bruta.new
objbruta.get_class_hierarchy_pos()
puts " Common characteristics of Bruta acquired from Mammalia"
#call to mammal class methods
objbruta.heart()
objbruta.senseorgan()
objbruta.lungs()
puts " Specific characteristics of Bruta"
objbruta.food()
objbruta.Motion()
objbruta.get_conservation_status()
# Create an object of Asian_Elephant Class and perform the actions
objasianelep=Asian_Elephant.new
objasianelep.get_class_hierarchy_pos()
puts " Common characteristics of Asian_Elephant acquired from Mammalia"
# call to mammal class methods
objasianelep.heart()
objasianelep.senseorgan()
objasianelep.lungs()
puts " Common characteristics of Asian_Elephant acquired from Bruta"
# call to mammal Bruta methods
objasianelep.food()
objasianelep.Motion()
puts " Specific characteristics of Asian_Elephant"
objasianelep.Trunk()
objasianelep.color()
objasianelep.size()
objasianelep.isaquatic()
objasianelep.intelligence()
objasianelep.get_status()
# Create an object of West_Indian_Manatee Class and perform the actions
objwest_Indian_Manatee=West_Indian_Manatee.new
objwest_Indian_Manatee.get_class_hierarchy_pos()
puts " Common characteristics of West_Indian_Manatee acquired from Mammalia"
# call to mammal class methods
objwest_Indian_Manatee.heart()
objwest_Indian_Manatee.senseorgan()
objwest_Indian_Manatee.lungs()
puts " Common characteristics of West_Indian_Manatee acquired from Bruta"
# call to mammal Bruta methods
objwest_Indian_Manatee.food()
objwest_Indian_Manatee.Motion()
puts " Specific characteristics of West_Indian_Manatee"
objwest_Indian_Manatee.color()
objwest_Indian_Manatee.size()
objwest_Indian_Manatee.isaquatic()
objwest_Indian_Manatee.get_status()
Save the main program along with the code for classes in to a text file and save it .rb extension.
Step 3-Run the program:
Now, got the path where the .rb file is saved from the last step and run following command ruby <<filename.rb>> as shown in the screen shot and hit enter
You would see following output of the program
Note: increase the font size to read the output
Step 4-Understand the output:
We will examine each part of the output and would map it with our main program to understand it more clearly.
First Part of the output is created from the code present under “ Create an object of Root class i.e Mammalia class and perform the actions” heading in the above section
Second part of the output is created from the code present under “Create an object of Bruta Class and perform the actions” heading in the above section
Third part of the output is created from the code present under “Create an object of Asian_Elephant Class and perform the actions ” heading in the above section
Fourth part of the output is created from the code present under “Create an object of West_Indian_Manatee Class and perform the actions ” heading in the above section
Source Code
#This is base class for our program
class Mammalia
# initializing constructor for the class
def initialize(class_hierarchical_pos="root class:Mammalia",status="")
@class_hierarchical_pos = class_hierarchical_pos
@status=status
end
def get_class_hierarchy_pos
puts "This class is #{@class_hierarchical_pos}"
end
def heart
puts " ->Heart with 2 auricles and 2 ventricles;contains Warm, dark red blood."
end
def lungs
puts " ->Lungs Respires alternately"
end
def senseorgan
puts " ->Has organ of sense such as nostrils,eyes etc"
end
# This method Returns the Conservation status for the mammal
def get_conservation_status(status="")
if status==""
puts " ->Conservation status is: Unknown, until we go to specific mammal under the hierarchy"
elsif status=="EX"
puts " ->Conservation status is: Extinct"
elsif status=="EW"
puts " ->Conservation status is: Extinct"
elsif status=="CR"
puts " ->Conservation status is: Extinct"
elsif status=="EN"
puts " ->Conservation status is: Endangered"
elsif status=="VU"
puts " ->Conservation status is: Extinct"
elsif status=="NT"
puts " ->Conservation status is: Extinct"
elsif status=="LC"
puts " ->Conservation status is: Least concerned"
else
puts " ->Invalid Conservation status passed."
end
end
def Supports
puts " ->Supports 4 feet except in aquatic"
end
end
class Bruta<Mammalia
# initializing constructor for the class
def initialize(class_hierarchical_pos="Bruta ,which is Subclass of Mammalia(Mammalia->BRUTA)")
@class_hierarchical_pos = class_hierarchical_pos
super(class_hierarchical_pos)
end
# Specific characteristics of Class
def food
puts " ->Food is mostly vegetables or plant species"
end
def Motion
puts " ->Motion of this category animals' is generally: Slow"
end
protected
# This method prints whether mammal is aquatic or not based on the param passed.
def isaquatic(param)
if param=="YES"
puts " ->This is aquatic mammal"
elsif param=="NO"
puts " ->This is not aquatic mammal"
end
end
end
class Asian_Elephant< Bruta
# initializing constructor for the class
def initialize(class_hierarchical_pos="Asian_Elephant,which is Subclass of Bruta(ROOT->BRUTA->Asian_Elephant)")
super(class_hierarchical_pos)
end
# Specific characteristics of Class
def get_status()
get_conservation_status("EN")
end
def Trunk
puts " ->Asian Elephants have trunk :The distinctive trunk is an elongation of the nose and upper lip combined"
end
def intelligence
puts " ->Asian Elephants are highly intelligent and self-aware"
end
def size
puts " ->Largest Asian Elephant recorded weighed 8 tonnes , stood 3.35 m (11 ft) tall at the shoulders and was 8.06 m long from head to tail."
end
def color
puts " ->Skin color is usually gray, and may be masked by soil because of dusting and wallowing."
end
def isaquatic
# call the super method in bruta class
super("NO")
end
end
class West_Indian_Manatee< Bruta
# initializing constructor for the class
def initialize(class_hierarchical_pos="is West_Indian_Manatee,which is Subclass of Root(ROOT->BRUTA->West_Indian_Manatee)")
super(class_hierarchical_pos)
end
# Specific characteristics of Class
def get_status
get_conservation_status("VU")
end
def isaquatic
super("yes")
end
def size
puts " ->The largest individual on record weighed 1,655 kg (3,649 lb) and measured 4.6 m (15 ft) long."
end
def color
puts " ->Color is grey and brown"
end
def isaquatic
# call the super method in bruta class
super("YES")
end
end
# Create an object of Root class i.e Mammalia class and perform the actions
objmammalia=Mammalia.new
objmammalia.get_class_hierarchy_pos()
puts " Characteristics of Mammalia:"
objmammalia.heart()
objmammalia.lungs()
objmammalia.senseorgan()
objmammalia.Supports()
# Create an object of Bruta Class and perform the actions
objbruta=Bruta.new
objbruta.get_class_hierarchy_pos()
puts " Common characteristics of Bruta acquired from Mammalia"
#call to mammal class methods
objbruta.heart()
objbruta.senseorgan()
objbruta.lungs()
puts " Specific characteristics of Bruta"
objbruta.food()
objbruta.Motion()
objbruta.get_conservation_status()
# Create an object of Asian_Elephant Class and perform the actions
objasianelep=Asian_Elephant.new
objasianelep.get_class_hierarchy_pos()
puts " Common characteristics of Asian_Elephant acquired from Mammalia"
# call to mammal class methods
objasianelep.heart()
objasianelep.senseorgan()
objasianelep.lungs()
puts " Common characteristics of Asian_Elephant acquired from Bruta"
# call to mammal Bruta methods
objasianelep.food()
objasianelep.Motion()
puts " Specific characteristics of Asian_Elephant"
objasianelep.Trunk()
objasianelep.color()
objasianelep.size()
objasianelep.isaquatic()
objasianelep.intelligence()
objasianelep.get_status()
# Create an object of West_Indian_Manatee Class and perform the actions
objwest_Indian_Manatee=West_Indian_Manatee.new
objwest_Indian_Manatee.get_class_hierarchy_pos()
puts " Common characteristics of West_Indian_Manatee acquired from Mammalia"
# call to mammal class methods
objwest_Indian_Manatee.heart()
objwest_Indian_Manatee.senseorgan()
objwest_Indian_Manatee.lungs()
puts " Common characteristics of West_Indian_Manatee acquired from Bruta"
# call to mammal Bruta methods
objwest_Indian_Manatee.food()
objwest_Indian_Manatee.Motion()
puts " Specific characteristics of West_Indian_Manatee"
objwest_Indian_Manatee.color()
objwest_Indian_Manatee.size()
objwest_Indian_Manatee.isaquatic()
objwest_Indian_Manatee.get_status()
No comments:
Post a Comment