Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Ruby classes and objects


May 12, 2021 Ruby


Table of contents


Ruby classes and objects

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

  • Data encapsulation
  • Data abstraction
  • Polymorphism
  • Inherited

These features are discussed in object-oriented Ruby.

An object-oriented program that involves classes and objects. C lasses are blueprints created by individual objects. In object-oriented terminology, your bike is an example of a bike class.

In the case of a vehicle, it includes wheels, horsepower, fuel or gas tank capacity. T hese properties form the data members of the Vehicle class. With these properties you can distinguish one vehicle from another.

The vehicle can also contain specific functions, such as halting, driving, and speeding. T hese functions form the data members of the Vehicle class. Therefore, you can define classes 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
   {
   }
}

By assigning different values to these data members, you can create different instances of the class Vehicle. F or example, an aircraft has three wheels, a horsepower of 1,000, and a fuel tank capacity of 100 liters. In the same way, a car has four wheels, a horsepower of 200 and a gas canister capacity of 25 liters.

Define the class 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, the class always starts with the keyword class, followed by the name of the class. T he first letter of the class name should be capital. Class Customer looks like this:

class Customer
end

You can use the keyword end to terminate a class. All data members in the class are somewhere between the class definition and the end keyword.

The variable in the Ruby class

Ruby provides four types of variables:

  • Local variable: A local variable is a variable defined in a method. L ocal variables are not available outside the method. I n a later section, you'll see more details about the method. Local variables start with lowercase letters or .
  • Instance variables: Instance variables can be used across methods in any particular instance or object. T his means that instance variables can change from object to object. The instance variable places the symbol before the variable name.
  • Class variables: Class variables can be used across different objects. A class variable belongs to a class and is a property of a class. Class variables place symbols before variable names.
  • Global variables: Class variables cannot be used across classes. I f you want to have a variable that you can use across classes, you need to define a global variable. Global variables always start with the dollar sign ($).

Using the class variable @@no_of_customers, you can determine the number of objects created, which determines the number of customers.

class Customer
   @@no_of_customers=0
end

Use the new method to create objects in Ruby

An object is an instance of a class. N ow you'll learn how to create class objects in Ruby. In Ruby, you can create objects using the class's method new.

Method new is a unique method that is predefined in the Ruby library. The new method belongs to the class method.

The following example creates two objects for the class Customer, cust1 and cust2:

cust1 = Customer. new
cust2 = Customer. new

Here, cust1 and cust2 are the names of the two objects. T he object name is followed by an equal sign, followed by the class name, followed by the dot operator and the keyword new.

Custom methods to create Ruby objects

You can pass parameters to the method new that can be used to initialize class variables.

When you want to declare a new method with parameters, you need to declare the method initialize at the same time as you create the class.

The initialize method is a special type of method that is 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 an initialize method with id, name, and addr as local variables. H ere, def and end are used to define the Ruby method initialize. In a later section, you'll learn more about the method.

In the initialize method, the values of these local variables are passed to the instance variables @cust_id, @cust_name, @cust_addr. Here, the value of the local variable is passed with the new method.

Now you can create an object that looks like this:

cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")

A member function in a 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 begin 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 part of the body of the method function within the class Sample. T hese 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 instance creates an object for the class Sample and calls the hello method:

#!/usr/bin/ruby

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

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

This will produce the following results:

Hello Ruby!

Simple case studies

If you want to do more exercises on classes and objects, here's a case study:

Ruby class case