Ruby Classes and Objects
Ruby is a pure object-oriented programming language. The features of object-oriented programming languages include:
- Data encapsulation
- data abstraction
- Polymorphism
- inheritance
These features are discussed in the "Object-oriented Ruby" section.
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 bicycle class.
Take a vehicle as an example: it has 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 have specific functions such as halting, driving, and speeding. These functions form the data members of the Vehicle class. Thus, you can define a class as a combination of attributes and functions.
The Vehicle class is defined 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 in Ruby, you first need to learn how to create objects and classes in Ruby.
In Ruby, a class always begins with the keyword class , followed by the class name. The class name should start with a capital letter. A class Customer As shown below:
class
Customer
end
You can use the keyword end to terminate a class.class All data members in are between the class definition and the end keyword.
Variables in Ruby classes
Ruby provides four types of variables:
- **Local variables:** Local variables are variables defined within a method. They are not available outside the method. In later chapters, you'll see more details about methods. Local variables begin with a lowercase letter or _.
- **Instance variables:** Instance variables can be used across methods within any particular instance or object. This means instance variables can vary from object to object. An instance variable is prefixed with the symbol (@).
- **Class variables:** Class variables can be used across different objects. A class variable belongs to the class and is an attribute of the class. A class variable is prefixed with 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 always start with a dollar sign ($).
Example
Using the class variable @@no_of_customers, you can determine the number of objects created, and thus the number of customers.
Example
class
Customer
@@no_of_customers
=
0
end
In Ruby, use the new method to create objects
An object is an instance of a class. Now you'll learn how to create objects of a class in Ruby. In Ruby, you can use the class's new method to create objects.
Method new is a unique method predefined in the Ruby library. The new method belongs toclassmethod.
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 (=), then the class name, then the dot operator and the keyword new。
Custom method to create Ruby objects
You can pass parameters to the method new ; these parameters can be used to initialize class variables.
When you want to declare the new method with parameters, you need to declare the method when you create the class initialize。
initialize method is a special type of method that is executed when the class's new method is called with arguments.
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 id、name、addr as local variables in the initializemethod. Here,def and end is used to define a Ruby method initialize. In later chapters, you'll learn more about methods.
in initialize In the method, pass the values of these local variables 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
"
)
Member functions in Ruby classes
In Ruby, functions are called methods.classEvery method in is started with the keyword def , followed by the method name.
Method names always start withLowercase letters. 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 is a method inside class Sample function and part of its body. 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 class Sample and call hello method in the component:
!/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!
A simple case study
If you want more practice with classes and objects, here's a case study: