Ruby Classes and Objects
Ruby is a perfect object-oriented programming language. Features of object-oriented programming languages include:
- Data Encapsulation
- Data abstraction
In the instance of a vehicle, it incorporates wheels, torque, fuel or gas tank limit. These characteristics structure the information individuals from the Vehicle class. With these characteristics you can recognize a vehicle from different vehicles.
Vehicles can likewise contain explicit capacities, for example, ending, driving, and speeding. These capacities structure the information individuals from the Vehicle class. Consequently, you can characterize a class as a blend of properties and capacities.
Class Vehicle is characterized as follows:
By doling out various qualities to these information individuals, you can make distinctive occasions of the class Vehicle. For instance, a plane has three wheels, 1,000 pull, and a fuel tank limit of 100 liters. Similarly, a vehicle has four wheels, a torque of 200, and a gas tank limit of 25 liters.
Defining classes in Ruby
To execute object-situated programming with Ruby, you have to initially figure out how to make items and classes in Ruby.
In Ruby, classes dependably begin with the watchword class pursued by the name of the class. The principal letter of the class name ought to be promoted. The class Customer looks like this:
You can terminate a class with the keyword end. All data members in the class are between the class definition and the end keyword.
Variables in the Ruby class
Ruby provides four types of variables:
- Local variables: Local factors are factors characterized in a technique. Nearby factors are not accessible outside of the strategy. In the accompanying sections, you will see more insights regarding the strategy. Nearby factors begin with a lowercase letter or _.
- Instance Variables: Instance factors can be utilized crosswise over techniques in a specific case or article. This implies example factors can change from article to question. The occasion variable places an image (@) before the variable name.
- Class variables: Class factors can be utilized crosswise over various items. A class variable has a place with a class and is a property of the class. Class factors place images (@@) before the variable name.
- Global Variables: Class factors can't be utilized crosswise over classes. On the off chance that you need to have a variable that can be utilized crosswise over classes, you have to characterize a worldwide variable. Worldwide factors dependably begin with a dollar sign ($).
Using the class variable @@no_of_customers, you can decide the quantity of articles being made so you can decide the quantity of clients.
Create questions in Ruby utilizing the new method
An object is an occasion of a class. Presently you will figure out how to make objects of a class in Ruby. In Ruby, you can make objects utilizing the class' strategy new .
Methods new is a one of a kind strategy that is predefined in the Ruby library. The new strategy has a place with the class technique.
The following precedent makes two objects of class Customer cust1 and cust2:
Here, cust1 and cust2 are the names of the two items. The article name is trailed by an equivalent sign (=) trailed by the class name, trailed by the dab administrator and the watchword new.
Custom strategies to make Ruby objects
You can pass contentions to strategy new , which can be utilized to instate class factors.
When you need to pronounce a new technique with parameters, you have to proclaim the strategy initialize while making the class.The
initialize strategy is an uncommon kind of technique that will be executed when the new technique for a class with parameters is called.
The following precedent makes the instate method:
In this model, you can proclaim a initialize technique with id, name, addr as a neighborhood variable. Here, def and end are utilized to characterize the Ruby strategy initialize. In the accompanying sections, you will study the strategies.
In the initialize technique, pass the estimations of these nearby factors to the occurrence factors @cust_id, @cust_name, and @cust_addr. Here, the estimation of the neighborhood variable is passed alongside the new strategy.
Now, you can make an item like this:
Member works in the Ruby class
In Ruby, capacities are called strategies. Every technique in the class begins with the watchword def pursued by the strategy name.
Method names dependably start with lowercase letters. In Ruby, you can end a technique with the watchword end.
The following model characterizes a Ruby method:
The following precedent makes an object of class Sample and calls the hello method:
This will create the accompanying results:
Simple case study
If you need to accomplish more activities on classes and items, here is a case study: