Ruby classes and objects


Ruby is a perfect object-oriented programming language. Features of object-oriented programming languages ​​include:

  • Data encapsulation

  • Data abstraction

  • Polymorphism Properties

  • Inheritance

These features will be discussed in the context of object-oriented Ruby.

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.

Take a vehicle as an example, it includes wheels, horsepower, 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.

Class Vehicle is defined as follows:

Class Vehicle
{
   Number no_of_wheels
   Number horsepower
   Characters type_of_tank
   Number Capacity
   Function speeding
   {
   }
   Function driving
   {
   }
   Function halting
   {
   }
}

You can create different instances of class Vehicle 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, 200 horsepower and a gas tank 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 start with the keyword class, followed by the name of the class. The first letter of the class name should be capitalized. Class Customer looks like this:

class Customer
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:

  • Local variables:Local variables are Variables defined in methods. 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 _.

  • Instance Variables: Instance variables can be used across any specific instance or method within an object. This means that instance variables can change from object to object. Instance variables place 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 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, so you can determine the number of customers.

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 objects of classes in Ruby. In Ruby, you create objects using the class's method new.

Method new is a unique method, predefined in the Ruby library. The new method belongs to the 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 the 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 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 when creating the class. The

initialize method is a special type of method that will be executed when the new method of a 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 initialize with id, name, addr as local variables method. 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 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 class starts with the keyword def, followed by the method name.

Method names always start with lowercase letters. In Ruby, you can end a method using 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 methods within class Sample## Part of the body of #function. These statements can be any valid Ruby statement. 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

hello Method:

#!/usr/bin/ruby

class Sample
   def hello
      puts "Hello Ruby!"
   end
end

# 使用上面的类来创建对象
object = Sample. new
object.hello

This will produce the following result:

Hello Ruby!

Simple case study

If you want to do more about classes and objects For exercises, here is a case study:

Ruby Class Case