Ruby is a perfect object-oriented programming language. Features of object-oriented programming languages ?? include:
These features in object-oriented Ruby for discussion.
- Data encapsulation
- Data abstraction
An object-oriented program involves classes and objects. Classes are blueprints created by individual objects. In object-oriented terminology, your bicycle is an instance of the bicycle class.
Taking a vehicle as an example, it includes wheels, horsepower, fuel or gas tank capacity. These attributes form data members of the Vehicle category. With these attributes you can distinguish a vehicle from other vehicles.
Vehicles can also contain specific functions, such as halting, driving, and speeding. These functions form data members of the Vehicle class. Therefore, you can define a class as a combination of attributes and functions.
The definition of class Vehicle is as follows:
By assigning different values ??to these data members, you can create different instances of the class Vehicle. For example, an airplane has three wheels, 1,000 horsepower, and a fuel tank capacity of 100 liters. In the same way, a car has four wheels, 200 horsepower, and a gas tank capacity of 25 liters.
Define classes in Ruby
In order to implement object-oriented programming with Ruby, you need to first learn how to create objects and classes in Ruby.
In Ruby, classes always start with the keyword class , followed by the name of the class. The first letter of the class name should be capitalized. The class Customer is as follows:
Variables in the Ruby class
Ruby provides four types of variables:
- Local variables: Local variables are variables defined in the method. Local variables are not available outside the method. In subsequent chapters, you will see more details about the method. Local variables start with a lowercase letter or _.
- Instance variables: Instance variables can be used across methods in any particular instance or object. This means that instance variables can change from object to object. Instance variables put a symbol (@) before the variable name.
- Class variables: Class variables can be used across different objects. Class variables belong to a class and are an attribute of the class. Class variables put a symbol (@@) before the variable name.
- Global variables: Class variables cannot be used across classes. If you want to have a variable that can be used across classes, you need to define a global variable. Global variables always start with a dollar sign ($).
class Customer @@no_of_customers = 0 end
Use the new method to create objects in Ruby
Objects are instances of classes. Now you will learn how to create class objects in Ruby. In Ruby, you can use the class method new to create objects.
The method new is a unique method, predefined in the Ruby library. The new method is a class method.
The following example creates two objects cust1 and cust2 of class Customer:
cust1 = Customer. new cust2 = Customer. new
Here, cust1 and cust2 are the names of two objects. The object name is followed by an equal sign (=), the equal sign is followed by the class name, then the dot operator and the keyword new .
Custom methods to create Ruby objects
You can pass parameters to the method new , which can be used to initialize class variables.
When you want to declare a new method with parameters , you need to declare the method initialize while creating the class .
The initialize method is a special type of method that will be executed when the new method of the class with parameters is called .
The following example creates the initialize method:
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 . In subsequent chapters, you will learn more details about methods.
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 value of the local variable is 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")
Member functions in the Ruby class
In Ruby, functions are called methods. Each method in the 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 in the Sample class . These statements can be any valid Ruby statements. For example, we can use the method puts to output Hello Ruby as follows:
class Sample def hello puts "Hello Ruby!" end end
The following example will create an object of class Sample and call the hello method:
#!/usr/bin/ruby class Sample def hello puts "Hello Ruby!" end end # Now using above class to create objects object = Sample. new object.hello
This will produce the following result:
Hello Ruby !
Simple case study
If you want to do more exercises on classes and objects, here is a case study: