English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Ruby Classes and Objects

Ruby is a perfect object-oriented programming language. The features of object-oriented programming languages include:

  • Data encapsulation
  • Data abstraction
  • Polymorphism
  • Inheritance

These features will be Object-oriented Ruby is discussed.

An object-oriented program, involving classes and objects. A class is a blueprint for creating individual objects. In object-oriented terminology, your bicycle is an example of the bicycle class.

Taking vehicles as an example, they include wheels (wheels), horsepower (horsepower), and fuel or gas tank capacity (fuel or gas tank capacity). These properties form the data members of the Vehicle class. With these properties, you can distinguish one vehicle from another.

Vehicles can also include specific functions, such as halting (halting), driving (driving), and speeding (speeding). These functions form the data members of the Vehicle class. Therefore, you can define the class as a combination of properties and functions.

The definition of class Vehicle is as follows:

Online 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 class Vehicle. For example, an airplane has three wheels, horsepower 1,000, the fuel tank capacity is 100 liters. In the same way, a car has four wheels, horsepower 200, the gas tank capacity is 25 Rise.

Defining classes in Ruby

To implement object-oriented programming in Ruby, you need to first learn how to create objects and classes in Ruby.

In Ruby, classes always start with the keyword class Start, followed by the class name. The first letter of the class name should be capitalized. The Customer As shown below:

class Customer
end

You can use the keyword end End a class.class All data members are between the class definition and end Between keywords.

Variables in Ruby classes

Ruby provides four types of variables:

  • Local variables:Local variables are variables defined within a method. Local variables are not available outside of the method. In subsequent chapters, you will see more details about methods. Local variables start with a lowercase letter or _.
  • Example variables:Example variables can be used across any specific example or object's methods. This means that example variables can change from object to object. Example variables are prefixed with the symbol (@).
  • 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 are prefixed with the symbol (@@).
  • 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 the dollar sign ($).

Online Example

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

Online Example

class Customer
   @@no_of_customers=0
end

In Ruby, using new method to create objects

An object is an instance of a class. Now you will learn how to create class objects in Ruby. In Ruby, you can use class methods new to create objects.

methods new is a unique method predefined in the Ruby library. The new method belongs toclassmethod.

The following example creates two objects cust1 and cust2:

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 (=), followed by the class name, and then the dot operator and the keyword new.

custom methods to create Ruby objects

You can give a method new pass the parameters, which can be used to initialize class variables.

when you want to declare a method with parameters new When you need to declare a method initialize.

initialize method is a special type of method that is called when a class with parameters is new is executed when the method is called.

The following example creates the initialize method:

Online 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 with id, name, addr as a local variable initializemethod. Here,def and end used to define Ruby methods initialize. In subsequent chapters, you will learn more about the details of methods.

In initialize In the method, pass the values of these local variables to the sample 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 an object 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.classEach method in def followed by the method name.

The method name always starts withlowercase lettersstart. 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 within the class Sample function is part of the main body. These statements can be any valid Ruby statements. For example, we can use the method puts to output Hello Rubyas shown below:

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

The following example will create an object of the class Sample and call 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 do more exercises on classes and objects, here is a case study:

Ruby Class Examples