Ruby Class Case
The following will make a Ruby class considered Customer that announces two methods:
- display_details: This strategy is utilized to show the subtleties of the client.
- total_no_of_customers: This technique is utilized to show the complete number of clients made in the framework.
display_details technique contains three puts proclamations that show the client ID, client name, and client address. Where the puts statement:
The estimation of the content Customer id and the variable @cust_id will be shown on a solitary line.
When you need to show the content and estimations of a case variable on a solitary line, you have to put the image (#) before the variable name of the puts proclamation. Content and occurrence factors with an image (#) ought to be encased in twofold statements.
The second strategy, total_no_of_customers, contains the class variable @@no_of_customers. Articulation @@no_of_ Customers+=1 increases the variable no_of_customers by 1 each time the strategy total_no_of_customers is called. Along these lines, you will get the complete number of clients in the class variable.
Now make two clients as follows:
Here, we made two objects of the Customer class, cust1 and cust2, and passed the essential parameters to the new strategy. At the point when the instate strategy is called, the important properties of the article are introduced.
Once an item is made, you have to utilize two articles to call the strategies for the class. On the off chance that you need to call a technique or any information part, you can compose the code as follows:
The object name is constantly trailed by a dab, trailed by the strategy name or information part. We have perceived how to call two techniques utilizing the cust1 object. With the cust2 object, you can likewise call two techniques as follows:
Save and execute code
Now, put all the source code in the main.rb file as follows:
Next, run the program as follows:
$ ruby main.rb
This will produce the following results:
Customer id 1 Customer name John Customer address Wisdom Apartments, Ludhiya Total number of customers: 1 Customer id 2 Customer name Poul Customer address New Empire road, Khandala Total number of customers: 2