Ruby is a perfect object-oriented programming language. The features of object-oriented programming languages include:
- Data encapsulation
- Data abstraction
- Polymorphism
- Inheritance
These features will be discussed in Object-Oriented Ruby.
An object-oriented program involves classes and objects. A class is a blueprint for creating individual objects. In object-oriented terminology, your bicycle is an instance of the bicycle class.
Take a vehicle as an example; it includes wheels, horsepower, and fuel or gas tank capacity. These attributes form the data members of the Vehicle class. With these attributes, you can distinguish one vehicle from another.
A vehicle can also contain specific functions, such as halting, driving, and speeding. These functions form the data members of the Vehicle class. Therefore, you can define a class as a combination of attributes and functions.
The definition of the Vehicle class is as follows:
Example
Class Vehicle {
Number no_of_wheels
Number horsepower
Characters type_of_tank
Number Capacity
Function speeding { }
Function driving { }
Function halting { }
}
By assigning different values to these data members, you can create different instances of the Vehicle class. For example, an airplane has three wheels, 1,000 horsepower, and a fuel tank capacity of 100 liters. Similarly, a car has four wheels, 200 horsepower, and a gas tank capacity of 25 liters.
To implement object-oriented programming in Ruby, you first need to learn how to create objects and classes in Ruby.
In Ruby, a class always starts with the keyword class, followed by the class name. The first letter of the class name should be capitalized. The Customer class is shown below:
class Customer
end
You can use the keyword end to terminate a class. All data members in the class are between the class definition and the end keyword.
Ruby provides four types of variables:
- Local Variables: Local variables are variables defined within methods. Local variables are not available outside the method. You will see more details about methods in subsequent chapters. Local variables start with a lowercase letter or _.
- Instance Variables: Instance variables can be used across any specific instance or object's methods. This means that instance variables can change from object to object. Instance variables are prefixed with the symbol (@) before the variable name.
- Class Variables: Class variables can be used across different objects. Class variables belong to the class and are an attribute of the class. Class variables are prefixed with the symbol (@@) before the variable name.
- Global Variables: Class variables cannot be used across classes. If you want a variable that can be used across classes, you need to define a global variable. Global variables always start with the dollar sign ($).
Example
Using the class variable @@no_of_customers, you can determine the number of objects created, which allows you to determine the number of customers.
Example
class Customer
@@no_of_customers = 0
end
An object is an instance of a class. Now you will learn how to create objects of a class in Ruby. In Ruby, you can create objects using the class method new.
The new method is a unique method that is predefined in the Ruby library. The new method belongs to the class method.
The following example creates two objects, cust1 and cust2, of the Customer class:
cust1 = Customer.new
cust2 = Customer.new
Here, cust1 and cust2 are the names of the two objects. The object name is followed by an equals sign (=), followed by the class name, then the dot operator and the keyword new.
You can pass parameters to the new method, which can be used to initialize class variables.
When you want to declare the new method with parameters, you need to declare the initialize method while creating the class.
The initialize method is a special type of method that is executed when the new method of the class with parameters is called.
The following example creates the initialize method:
Example
class Customer
@@no_of_customers = 0
def initialize(id, name, addr)
@cust_id = id
@cust_name = name
@cust_addr = addr
end
end
In this example, you can declare the initialize method with id, name, addr as local variables. Here, def and end are used to define the Ruby method initialize. You will learn more details about methods in subsequent chapters.
In the initialize method, the values of these local variables are passed to the instance variables @cust_id, @cust_name, and @cust_addr. Here, the values of the local variables are passed along with the new method.
Now, you can create objects as follows:
cust1 = Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2 = Customer.new("2", "Poul", "New Empire road, Khandala")
In Ruby, functions are called methods. Every method in a class starts with the keyword def, followed by the method name.
Method names always start with a lowercase letter. In Ruby, you can use the keyword end to end a method.
The following example defines a Ruby method:
class Sample
def function
statement 1
statement 2
end
end
Here, statement 1 and statement 2 are part of the body of the method function within the Sample class. These statements can be any valid Ruby statements. For example, we can use the method puts to output Hello Ruby, as shown below:
class Sample
def hello
puts "Hello Ruby!"
end
end
The following example will create an object of the Sample class and call the hello method:
class Sample
def hello
puts "Hello Ruby!"
end
end
object = Sample.new
object.hello
This will produce the following result:
Hello Ruby!
If you want to practice more about classes and objects, here is a case study:
YouTip