Ruby classes and objects
Ruby classes and objects
Ruby is a perfect object-oriented programming language. Features of object-oriented programming languages include:
- Data encapsulation
- polymorphism
- data abstraction
- inherit
These features will be discussed later.
An object-oriented program involves classes and objects. Classes are blueprints for the creation of individual objects. In object-oriented terms, your bicycle is an instance of the Bicycle class.
Taking a vehicle as an example, it includes wheels, horsepower, and fuel or gas tank capacity. These properties form the data members of the Vehicle class. With the help of these attributes you can distinguish one vehicle from other vehicles.
Vehicles 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 properties and functions.
The class Vehicle is defined as follows:
Example
2. {
3. Number no_of_wheels
4. Number horsepower
5. Characters type_of_tank
6. Number Capacity
7.
8. Function speeding
9. {
10. }
11.
12. Function driving
13. {
14. }
15.
16. Function halting
17. {
18. }
19. }

You can create different instances of the Vehicle class by assigning different values to these data members. 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, a horsepower of 200, and a gas tank with a capacity of 25 liters.
Defining classes in Ruby
In order to implement object-oriented programming using Ruby , you need to first learn how to create objects and classes in Ruby.
In Ruby, classes always begin with the keyword class , followed by the name of the class. The first letter of the class name should be capitalized. The class Customer looks like this:
2. end
You can terminate a class using the keyword end . All data members in a class are between the class definition and the end keyword.
Variables in Ruby classes
Ruby provides four types of variables:
- Instance variables: Instance variables can be used across any specific instance or method in an object. This means that instance variables can change from object to object. Instance variables place the symbol (@) before the variable name.
- Local variables: Local variables are variables defined within a method. Local variables are not available outside methods. In subsequent chapters you will see more details about the method. Local variables start with a lowercase letter or _.
- 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 place the 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 ($).
Example
Using the class variable @@no_of_customers you can determine the number of objects created and thus determine the number of customers.
2. @@no_of_customers=0
3. end
Create objects using new method in Ruby
Objects are instances of classes. Now you'll learn how to create objects of classes in Ruby. In Ruby, you create objects using the new method of a class .
Method new is a unique method, predefined in the Ruby library. The new method is a class method.
The following example creates two objects of class Customer, cust1 and cust2:
2. cust2 = Customer. new
Here, cust1 and cust2 are the names of two objects. The object name is followed by an equal sign (=), which 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 new method , and these parameters 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:
Example
2. @@no_of_customers=0
3. def initialize(id, name, addr)
4. @cust_id=id
5. @cust_name=name
6. @cust_addr=addr
7. end
8. end
In this example, you can declare the initialize method withid, 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 the method.________
In the initialize method, pass the values of these local variables to the instance variables @cust_id, @cust_name and @cust_addr. Here, the value of the local variable is passed with the new method.
Now you can create the object as follows:
2. cust2=Customer.new("2", "Poul", "New Empire road, Khandala")
Member functions in Ruby classes
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:
2. def function
3. statement 1
4. statement 2
5. end
6. end
Here, statement 1 and statement 2 are part of the body of the method function within class Sample . These statements can be any valid Ruby statement. For example, we can use the method puts to output Hello Ruby as follows:
2. def hello
3. puts "Hello Ruby!"
4. end
5. end
The following example creates an object of class Sample and calls the hello method:
2.
3. class Sample
4. def hello
5. puts "Hello Ruby!"
6. end
7. end
8.
9. # 使用上面的类来创建对象
10. object = Sample. new
11. object.hello
This will produce the following results:
Hello Ruby!