Ruby Classes and Objects
Ruby is a perfect object-oriented programming language. The features of object-oriented programming 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 the blueprint from which individual objects are created. In object-oriented terms, your bicycle is an instance of the class of objects known as bicycles.
Taking the example of a vehicle, it includes attributes like 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.
Vehicles can also include 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 class Vehicle 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.
Defining Classes in Ruby
To implement object-oriented programming using Ruby, you 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 class name should start with a capital letter. The class Customer
is shown below:
class Customer
end
You can terminate a class by using the keyword end
. All data members in the class are between the class definition and the end
keyword.
Variables in Ruby Classes
Ruby provides four types of variables:
Local Variables: Local variables are defined within a method. Local variables are not available outside the method. Local variables start with a lowercase letter or _.
Instance Variables: Instance variables are available across methods for any particular instance or object. Instance variables change from object to object. Instance variables are preceded by the @ symbol.
Class Variables: Class variables are available across different objects. Class variables belong to the class and are a characteristic of the class. Class variables are preceded by the @@ symbol.
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 are always preceded by the dollar sign ($).
Example
Using the class variable @@no_of_customers, you can determine the number of objects created, which helps determine the number of customers.
Example
class Customer
@@no_of_customers = 0
end
Creating Objects in Ruby Using the new
Method
An object is an instance of a class. You will learn how to create objects of a class in Ruby. In Ruby, you can create objects using the new
method of the class.
The new
method is a unique type of method, predefined in the Ruby library. The new
method belongs to the class methods.
The following example creates two objects, cust1 and cust2, of the class Customer:
cust1 = Customer.new
cust2 = Customer.new
Here, cust1 and cust2 are the names of two objects. The object names are followed by the equal sign (=), which is followed by the class name, then the dot operator, and the keyword new
.
Custom Method to Create Ruby Objects
You can pass arguments to the new
method, 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
when creating the class.
The initialize
method is a special type of method that will be executed when the new
method of the class is called with parameters.
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 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 about methods in subsequent chapters.
In the initialize
method, the values of the local variables are assigned to the instance variables @cust_id, @cust_name, and @cust_addr. The local variables' values 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")
Member Functions in Ruby Classes
In Ruby, functions are called methods. Each method in a class starts with the keyword def
followed by the method name.
The method name always starts with a lowercase letter. In Ruby, you can end a method with the keyword end
.
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 method function
within the class Sample
. These statements can be any valid Ruby statements. For example, we can use the puts
method to output Hello Ruby
, as shown below:
class Sample
def hello
puts "Hello Ruby!"
end
end
The following example creates an object of the class Sample
and calls the hello
method:
#!/usr/bin/ruby
class Sample
def hello
puts "Hello Ruby!"
end
end
# Create an object using the above class
object = Sample.new
object.hello
This will produce the following result:
Hello Ruby!
Simple Case Study
If you want to practice more about classes and objects, here is a case study: