Ruby classes and objects


Release date:2023-10-26 Update date:2023-10-27 Editor:admin View counts:241

Label:

Ruby classes and objects

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

  • Data encapsulation

  • Data abstraction

  • Polymorphisms

  • Inherit

These features will be discussed in object-oriented Ruby.

An object-oriented program that involves classes and objects. A class is a blueprint created by individual objects. In object-oriented terms, your bike is an instance of the bicycle class.

Take a vehicle as an example, it includes wheels, horsepower (horsepower), fuel or fuel or gas tank capacity. These properties form the data members of the vehicle class. With these attributes, you can distinguish one vehicle from another.

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

ClassVehicle{Numberno_of_wheelsNumberhorsepowerCharacterstype_of_tankNumberCapacityFunctionspeeding{
}Functiondriving{ }Functionhalting{ } }

By assigning different values to these data members, you can create classes Vehicle different examples of. For example, an airplane has threewheels, horsepower 1,000 the capacity of the fuel tank is 100 L. In the same way, a car has four wheels, horsepower 200 the capacity of the gas tank is 25 L.

Define classes in Ruby

In order to use Ruby for object-oriented programming, you need to first learn how to create objects and classes in Ruby.

In Ruby, classes always use keywords class start, followed by the name of the class. The first letter of the class name should be capitalized. Class Customer as follows:

classCustomerend

You can use keywords end terminates a class. All data members in a class are between the class definition and the end between keywords.

Variables in the Ruby class

Ruby provides four types of variables:

  • Local variables: local variables are variables defined in the method. Local variables are not available outside the method. You will see more details about the approach in subsequent chapters. Local variables are in lowercase letters or _ start.

  • Instance variables: instance variables can be used across methods in any particular instance or object. This means that instance variables can be changed from object to object. The instance variable places the symbol before the variable name @ ).

  • Class variables: class variables can be used across different objects. The class variable belongs to the class and is an attribute of the class. Class variables place symbols 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 global variables. Global variables are always marked with the dollar sign $ ) begin.

Example

Use class variables @@no_of_customers can determine the number of objects being created, which determines the number of customers

Example

classCustomer@@no_of_customers=0end

Use in Ruby new Method to create an object

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

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

The following example creates a class Customer two objects of cust1 and cust2 :

cust1=Customer.newcust2=Customer.new

Here, cust1 And cust2 is the name of two objects. The object name is followed by the equal sign (=), followed by the class name, followed by the dot operator and keyword new .

Custom methods to create Ruby objects

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

When you want to declare a new method, you need to declare the method while creating the class initialize .

initialize method is a special type of method that will call the new method is executed.

The following example creates a initialize methods:

Example

classCustomer@@no_of_customers=0definitialize(id,name,addr)@cust_id=id@cust_name=name@cust_addr=addrendend

In this example, you can declare that you have idnameaddr as a local variable initialize Method. Here, def and end used to define Ruby methods initialize . In later chapters, youwill learn more about the method.

In initialize method, pass the values of these local variables to the instance variables @cust_id@cust_name and @cust_addr . In this case, the value of the local variable is with the new method to pass on.

You can now 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 the Ruby class

In Ruby, functions are called methods. Each method in the class is defined by a keyword def start, followed by the method name.

Method names always start with a lowercase letter. In Ruby, you can use keywords end to end a method.

The following example defines a Ruby method:

classSampledeffunctionstatement1statement2endend

Here, statement 1 and statement 2 are methods within the class Sample function . The main body of the component. These statements can beany valid Ruby statement. For example, we can use the method puts to output Hello Ruby , as follows:

classSampledefhelloputs"Hello Ruby!"endend

The following example creates a class Sample and call the hello methods:

#!/usr/bin/rubyclassSampledefhelloputs"Hello
Ruby!"endend#Use the above class to create objectsobject=Sample.newobject.hello

This will produce the following results:

Hello Ruby!

Powered by TorCMS (https://github.com/bukun/TorCMS).